1. 程式人生 > >STL語法——映射:map 反片語(Ananagrams,UVa 156)

STL語法——映射:map 反片語(Ananagrams,UVa 156)

count lower code cin mos abc 最終 定義 lead

Description

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

中文大意:

輸入一些單詞,找出所有滿足如下條件的單詞:該單詞不能通過字母重排,得到輸入文本中的另外一個單詞。
在判斷是否滿足條件時,字母不分大小寫,但在輸入時應保留輸入中的大小寫,按字典序進行排列(所有大寫字母在小寫字母的前面)

分析:

題意:

從若幹個單詞中找出只出現過一次的單詞,前提是不區分大小寫,並且字母的順序也不一定,例如:abc
和cba 就是屬於同一個單詞(因為題目是說值出現一次的單詞,所以代碼中必須有一個計數的變量)

思路:

判斷兩個單詞是否可以通過重排列得到,把兩個單詞排序,然後比較兩個單詞是否相同,若相同則可以通過重新排列得到,所以對每個輸入的單詞進行標準化,即把單詞中的字母轉化為小寫字母

(判斷單詞重排時,不區分大小寫)然後對該單詞進行排序,然後用map來儲存標準化後的單詞

1.寫一個標準化函數(實現大寫字母轉換為小寫(tolower()函數),單詞排序。註意使用const是為了不改變s的初值)
2.兩個vector容器(words,ans),一個map容器(cnt)
words存儲所有的單詞
map存儲標準化後對應單詞以及出現次數的值,相當於一個表格。
words經過查表map,把對應的符合值給ans
3.輸出

代碼:

 1 # include <iostream>//輸入輸出流
 2 # include <string>
 3 # include <cctype> //包含 tolower 函數
 4 # include <vector>  //不定長數組
 5 # include <map>     //映射,在本例中36行有體現
 6 # include <algorithm> //一些函數,sort等
 7 using namespace std;
 8 
 9 map<string, int> cnt; //定義映射
10 vector<string> words; //一個叫words的不定長數組
11 
12 //將單詞s進行標準化
13 string repr(const string& s)//聲明變量s
14 {
15     string ans = s;//定義字符串類型數組,並使用第三方變量進行轉換,並最終輸出時還能保留大寫部分
16     for (int i = 0; i < ans.length(); i++)
17         ans[i] = tolower(ans[i]);//全部轉化成小寫字母
18     sort(ans.begin(), ans.end());//從小到大按字典序進行排序
19     return ans;
20 }
21 
22 int main()
23 {
24     int n = 0;
25     string s;
26     while (cin >> s)
27     {
28         if (s[0] == #) break;//遇到‘#’結束循環
29         words.push_back(s);//存入vector
30         string r = repr(s);//給所有單詞進行排列,上面13行用“引用”的原因,方便調用
31         if (!cnt.count(r)) cnt[r] = 0; //!cnt.count(r) 的值,不是0就是1
32         cnt[r]++;      //count 是返回容器中r出現的次數,統計重新組成新單詞的個數
33     }
34     vector<string> ans;//又重新復制了一份,用另外一個存儲
35     for (int i = 0; i < words.size(); i++)
36       if (cnt[repr(words[i])] == 1)//此時用到了“映射”,如果不是映射的話,一個是字符串一個是int型,不能用等號的;找到唯一的單詞
37             ans.push_back(words[i]);
38     sort(ans.begin(), ans.end());//排序
39     for (int i = 0; i < ans.size(); i++)
40         cout << ans[i] << "\n";
41 
42     getchar();
43     getchar();
44     
45     return 0;
46 }

運行結果:

技術分享

革命尚未成功,同誌們仍需努力......

STL語法——映射:map 反片語(Ananagrams,UVa 156)