zoj 1166 Anagram Checker

来源:互联网 发布:easymule 软件 编辑:程序博客网 时间:2024/05/01 14:16
  1. /**
  2. A seach problem.
  3. **/
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <algorithm>
  9. using namespace std;
  10. /**
  11. dictionary words
  12. **/
  13. string word[2000];
  14. /**
  15. Word in phrase
  16. **/
  17. string sword[20];
  18. /**
  19. phrase sorted in alphabetic order
  20. **/
  21. string phrase;
  22. string oldphrase;
  23. /**
  24. Number of dictionary words, number of words in phrase.
  25. **/
  26. int n=0, m;
  27. /**
  28. Count of letters in phrase.
  29. **/
  30. int cont[26];
  31. void findAnagram(int idx, vector<string> &v)
  32. {
  33.      bool find=true;
  34.      for (int i=0; i<26; i++) 
  35.      {
  36.        if (cont[i] > 0)
  37.         { find = false;  break; }
  38.      }
  39.      if (find)
  40.      {
  41.         string s;
  42.         for (int i=0; i<v.size(); i++)
  43.         {
  44.             if (i > 0) s += " ";
  45.             s += v[i];
  46.         }
  47.         if (s != phrase) cout<<oldphrase<<" = "<<s<<endl;
  48.         return;
  49.      }
  50.      int cnt[26];
  51.      for (int i=idx; i<n; i++)
  52.      {
  53.          memset(cnt, 0, sizeof(cnt));
  54.          bool enough = true;
  55.          for (int j=0; j<word[i].length(); j++)
  56.          {
  57.             int a = word[i][j] - 'A';
  58.             if (cont[a] < cnt[a] + 1){ enough=falsebreak; }
  59.             else cnt[a]++;
  60.          }
  61.          
  62.          if (enough)
  63.          {
  64.              for (int j=0; j<26; j++)
  65.              {
  66.                  cont[j] -= cnt[j];
  67.              }
  68.              v.push_back(word[i]);
  69.              
  70.              findAnagram(i, v);
  71.                 
  72.              v.pop_back();
  73.              for (int j=0; j<26; j++)
  74.              {
  75.                  cont[j] += cnt[j];
  76.              }
  77.          }
  78.      }
  79. }
  80. int main(int argc, char *argv[])
  81. {
  82.     while (cin>>word[n], word[n] != "#") n++;
  83.     string s;
  84.     sort(word, word+n);
  85.     m=0;
  86.     while (1)
  87.     {
  88.           cin>>sword[m];
  89.           if ("#" == sword[m++]) break;
  90.           if (cin.get() == '/n')
  91.           {
  92.           /**
  93.           Keep the old phrase for output.
  94.           **/
  95.              oldphrase = "";
  96.              for (int i=0; i<m; i++)
  97.              {
  98.                  if (i) oldphrase += " ";
  99.                  oldphrase += sword[i];
  100.              }             
  101.              
  102.              /**
  103.              Sort words in phrase, for output check, don't output the Anagram 
  104.              same as phrase.
  105.              **/
  106.              sort(sword, sword+m);
  107.              phrase = "";
  108.              for (int i=0; i<m; i++)
  109.              {
  110.                  if (i) phrase += " ";
  111.                  phrase += sword[i];
  112.              }
  113.              
  114.              /**
  115.              Initialize count array. And calculate new values for new phrase.
  116.              **/
  117.              memset(cont, 0, sizeof(cont));
  118.              for (int i=0; i<phrase.length(); i++)
  119.              {
  120.                 if (phrase[i] == ' 'continue;
  121.                 cont[phrase[i] - 'A']++;
  122.              }
  123.              vector<string> v;
  124.              findAnagram(0, v);
  125.              m = 0;
  126.           }
  127.     }
  128. }