zoj 1166 Anagram Checker
来源:互联网 发布:easymule 软件 编辑:程序博客网 时间:2024/05/01 14:16
- #include <cstdlib>
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- string word[2000];
- string sword[20];
- string phrase;
- string oldphrase;
- int n=0, m;
- int cont[26];
- void findAnagram(int idx, vector<string> &v)
- {
- bool find=true;
- for (int i=0; i<26; i++)
- {
- if (cont[i] > 0)
- { find = false; break; }
- }
- if (find)
- {
- string s;
- for (int i=0; i<v.size(); i++)
- {
- if (i > 0) s += " ";
- s += v[i];
- }
- if (s != phrase) cout<<oldphrase<<" = "<<s<<endl;
- return;
- }
- int cnt[26];
- for (int i=idx; i<n; i++)
- {
- memset(cnt, 0, sizeof(cnt));
- bool enough = true;
- for (int j=0; j<word[i].length(); j++)
- {
- int a = word[i][j] - 'A';
- if (cont[a] < cnt[a] + 1){ enough=false; break; }
- else cnt[a]++;
- }
-
- if (enough)
- {
- for (int j=0; j<26; j++)
- {
- cont[j] -= cnt[j];
- }
- v.push_back(word[i]);
-
- findAnagram(i, v);
-
- v.pop_back();
- for (int j=0; j<26; j++)
- {
- cont[j] += cnt[j];
- }
- }
- }
- }
- int main(int argc, char *argv[])
- {
- while (cin>>word[n], word[n] != "#") n++;
- string s;
- sort(word, word+n);
- m=0;
- while (1)
- {
- cin>>sword[m];
- if ("#" == sword[m++]) break;
- if (cin.get() == '/n')
- {
-
- oldphrase = "";
- for (int i=0; i<m; i++)
- {
- if (i) oldphrase += " ";
- oldphrase += sword[i];
- }
-
-
- sort(sword, sword+m);
- phrase = "";
- for (int i=0; i<m; i++)
- {
- if (i) phrase += " ";
- phrase += sword[i];
- }
-
-
- memset(cont, 0, sizeof(cont));
- for (int i=0; i<phrase.length(); i++)
- {
- if (phrase[i] == ' ') continue;
- cont[phrase[i] - 'A']++;
- }
- vector<string> v;
- findAnagram(0, v);
- m = 0;
- }
- }
- }