permutation

 

bool _Permutation(char* str) {
  int len = strlen(str);
  for (int i = len - 2; i >= 0; --i) {
    if (str[i] < str[i + 1]) {
      for (int j = len - 1; j > i; --j) {
        if (str[j] > str[i]) {
          std::swap(str[i], str[j]);
          _strrev(str + i + 1);
          return true;
        }
      }
    }
  }
  _strrev(str); 
  return false;
}

void Permutation(char* str) {
  std::sort(str, str + strlen(str));
  do {
    std::cout << str << std::endl;
  } while (_Permutation(str));
}

 

posted @ 2013-11-17 19:40  avexer  阅读(164)  评论(0编辑  收藏  举报