close
195.Anagram
作者:foxs91092
#include#include #include #include using namespace std; const string strTable = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; int table[128]; main() { for (int i = 0; i < (int)strTable.size(); ++i){ table[strTable[i]] = i; } int loop; cin >> loop; while (loop--){ string in; cin >> in; vector v; for (string::iterator it = in.begin(); it != in.end(); ++it){ v.push_back(table[*it]); } sort(v.begin(), v.end()); do { for (vector ::iterator it = v.begin(); it != v.end(); ++it){ cout << strTable[*it]; } cout << endl; } while (next_permutation(v.begin(), v.end())); } }
#include#include #include #include #include #include #include #include #include struct Order{ bool operator () (const char & c1, const char & c2) const{ if(tolower(c1) != tolower(c2)){ return tolower(c1) < tolower(c2); } return c1 < c2; } }; void f(const std::string & s, bool history[], int depth, char * buffer){ if(depth == s.size()){ buffer[depth] = '\0'; std::cout << buffer << std::endl; return; } int prev = -1; for(int i = 0; i < s.size(); i++){ if(prev < 0 || prev != s[i]){ if(history[i] == false){ history[i] = true; buffer[depth] = s[i]; f(s, history, depth + 1, buffer); history[i] = false; prev = s[i]; }else{ prev = -1; } } } } int main(){ int m; std::cin >> m; std::string line; std::getline(std::cin, line); while(m--){ bool history[1000]; char buffer[1000]; char sorted[1000]; Order o; std::getline(std::cin, line); if(line.size() > 500){ std::cout << m / 0 << std::endl; } for(int i = 0; i < line.size(); i++){ history[i] = false; sorted[i] = line[i]; } sorted[line.size()] = '\0'; std::sort(sorted, sorted + line.size(), o); line = std::string(sorted); f(line, history, 0, buffer); } }
作者:foxs91092
#include#include #define MAX 100 char str[MAX]; char nowstr[MAX]; int nstr[MAX]; bool used[MAX]={false}; int len=0; int permu(int); int main() { int N; scanf("%d", &N); gets(str); for(int i=0; i =65&&str[i]<=90) nstr[i]=(str[i]-65)*2; else nstr[i]=(str[i]-97)*2+1; //convert bool sw=true; for(int i=0; i nstr[j+1]) { char temp=str[j]; str[j]=str[j+1]; str[j+1]=temp; int ntemp; ntemp=nstr[j]; nstr[j]=nstr[j+1]; nstr[j+1]=ntemp; sw=true; } //swap } //sort permu(0); } return 0; } int permu(int now_depth) { if(now_depth==len) { nowstr[len]='\0'; printf("%s\n", nowstr); return 0; } //permutation is over char last='\0'; for(int i=0; i
全站熱搜
留言列表