Ananagrams Uva 156
Ananagrams (map的使用)
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.
樣例輸入:
ladder came tape soon leader acme RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE derail LaCeS drIednoel dire Disk mace Rob dries
#
樣例輸出:
Disk
NotE
derail
drIed
eye
ladder
soon
分析:
用一個vector存儲出現過的單詞,把所有單詞統一成一個形式,即先把所有字母改為小寫,將該單詞排序,再放到map里統計可排列成相同單詞的數目,把出現次數為1的單詞輸出。
code
#include <iostream> #include <cstring> #include <algorithm> #include <string> #include <vector> #include <map> using namespace std;/* 將單詞標準化 */ string standard( string str ) {for( int i = 0; i < str.size(); i++ ) {str[i] = tolower( str[i] );}sort( str.begin(), str.end() ); //把單詞中的字母按字典序排列return str; }int main() {//freopen("input.txt", "r", stdin);vector<string> words; //存儲所有單詞map<string, int> _map; //記錄標準化后的單詞出現次數string str;while( cin >> str && str != "#" ) {words.push_back( str );if ( ! _map.count( standard(str) ) ) { //單詞未出現,將其數目標記為0_map[ standard(str) ] = 0; //可看作初始化,盡管該單詞已經出現,但此時標記為0即可,因為后續還會自加}_map[ standard(str) ]++; //因為該單詞出現了,所以次數加1(若首次出現,剛好0加1后變為1;若不是首次出現,次數加1)}vector<string> ans;vector<string>::iterator it;for( it = words.begin(); it != words.end(); it++ ) {if ( _map[ standard(*it) ] == 1 ) {ans.push_back( *it ); //將出現次數為1的單詞放到ans中}}sort( ans.begin(), ans.end() ); //按字典序排序for( it = ans.begin(); it != ans.end(); it++ ) {cout << *it << endl; //輸出} }總結
以上是生活随笔為你收集整理的Ananagrams Uva 156的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 明日方舟如何加固土石结构
- 下一篇: CodeForce 2A —— Winn