uvaoj-156:反片語

Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their letters, you cannot form another word. Such words are called ananagrams, an example is QUIZ.

 

Obviously such definitions depend on the domain within which we are working; you might think that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible domain would be the entire English language, but this could lead to some problems. One could restrict the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the same domain) but NOTE is not since it can produce TONE.

 

Write a program that will read in the dictionary of a restricted domain and determine the relative ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be ``rearranged'' at all. The dictionary will contain no more than 1000 words.

 

Input

Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken across lines. Spaces may appear freely around words, and at least one space separates multiple words on the same line. Note that words that contain the same letters but of differing case are considered to be anagrams of each other, thus tIeD and EdiT are anagrams. The file will be terminated by a line consisting of a single #.

 

Output

Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always be at least one relative ananagram.

 

Sample input

 

ladder came tape soon leader acme RIDE lone Dreis peat
 ScAlE orb  eye  Rides dealer  NotE derail LaCeS  drIed
noel dire Disk mace Rob dries
#

 

Sample output

 

Disk
NotE
derail
drIed
eye
ladder
soon


題解:題意大概就是把輸入的每個單詞按字母都變成小寫,再把字母按照字典序排列,構成一個新的單詞(也不一定是單詞啦,就是一大堆字母的集合);然後再和輸入的所有單詞比較,如果經過轉化以後的單詞在所有轉化後形成的的單詞組中不是唯一的,就將它算在答案以外,如果是唯一的,就存入答案序列中;最後按照字典序來輸出答案;具體分析在代碼中註釋;


筆記:最近做題純粹是爲了熟悉c++的STL模板,慢慢瞭解到了迭代器(iterator)(和指針非常類似),以及集合和映射;這個代碼利用了set的唯一性和有序性,主要是看着好看,用sort進行排列一下,再用不定長數組保存(就像流入家書上的那樣)也行,但我覺得利用set保存也算是一個小改進吧;



code:

#include <algorithm>
#include <map>//利用映射來存儲相同的單詞出現的次數
#include <string>
#include <vector>
#include <iostream>
#include <set>
using namespace std;

vector<string> words;//相當於二維字符數組;
map<string,int> cnt;

string sta(const string &s)//引用;
{
    string ans=s;//s是引用的,而且用const固定了,所以需要另外一個不定字符串來進行返回值的返回;
    int len=ans.size();//取長度,相當於cstring的strlen;
    for(int i=0; i<len; i++)
    ans[i]=tolower(ans[i]);//轉化小寫字母;
    sort(ans.begin(), ans.end());//將字母排序
    return ans;
}

int main()
{
    string s;
    string r;
    while(cin>>s&&s!="#")
    {
        words.push_back(s);
        r=sta(s);//r是處理以後的單詞;
        if(!cnt[r])
        {
            cnt[r]=0;//處理後的單詞沒有出現過的話就在map中創建一個鍵來賦值爲0;
        }
        cnt[r]++;//沒出現過的話值會變爲1,出現過的會大於1;
    }
    int len=words.size();//取單詞數目;
    set <string> ans;//創建集合來保存答案序列;
    for(int i=0; i<len; i++)
    {
        if(cnt[sta(words[i])]==1)//判定來確定是否將單詞保存進答案序列中;
        ans.insert(words[i]);//因爲集合中的數據都是pai'hao'xu'
    }
    set<string>::iterator it;//創建迭代器來進行循環,因爲set就是要這樣循環的。。
    for(it=ans.begin(); it!=ans.end(); ++it)
    cout<<*it<<endl;//可以理解爲指針吧?
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章