標籤:hdu map string
轉載請註明出處:http://blog.csdn.net/u012860063天資
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1113
來吧!!歡迎"熱愛編程"的同學報考杭電,期待你加入“杭電ACM集訓隊”! 7月22-8月21多校聯合訓練期間,會根據實際負載關閉部分模組,若有不便,請諒解~ |
Word AmalgamationTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2496 Accepted Submission(s): 1198
Problem DescriptionIn millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four words. Your task is to write a program that can unscramble words. InputThe input contains four parts:
1. a dictionary, which consists of at least one and at most 100 words, one per line; 2. a line containing XXXXXX, which signals the end of the dictionary; 3. one or more scrambled `words‘ that you must unscramble, each on a line by itself; and 4. another line containing XXXXXX, which signals the end of the file.
All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most six characters long. (Note that the sentinel XXXXXX contains uppercase X‘s.) The dictionary is not necessarily in sorted order, but each word in the dictionary is unique. OutputFor each scrambled word in the input, output an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. Each word in this list must appear on a line by itself. If the list is empty (because no dictionary words can be formed), output the line ``NOT A VALID WORD" instead. In either case, output a line containing six asterisks to signal the end of the list. Sample InputtarpgivenscorerefundonlytrapworkearncoursepepperpartXXXXXXresconfudreaptrsettoresucXXXXXX Sample Outputscore******refund******parttarptrap******NOT A VALID WORD******course****** SourceMid-Central USA 1998 RecommendEddy |
題意:先給你一些單詞作為字典,在給一系列的單詞尋找字典中是否有這些單詞(注意尋找的單詞,一個單詞中的字母順序是可以變得,也就是說單詞之間只要字母是一樣的不用考慮順序是否一樣都要輸出);
思路:用map和string變很容易解決,先把字典存入map裡,在逐一尋找就OK,當然尋找的時候需要一點小小的操作,詳見代碼解釋;
map的詳細用法:http://blog.csdn.net/u012860063/article/details/24435211
代碼如下(超級詳細版):
#include <cstdio>#include <map>#include <string>#include <iostream>#include <algorithm> using namespace std;int main(){map<string,string>m;//定義map的變數和值都為string類型 string t, s;while(cin >> s){if( s == "XXXXXX") {//註:string類型的是可以直接在字串之間用"="或"=="進行賦值或判斷的break;}t = s;sort(s.begin(),s.end());//直接對string類型的字串用begin()和end()進行排序 m[t] = s;}while(cin >> s){if( s == "XXXXXX"){break;}int flag = 0;//用於記錄字典裡是否有要查詢的單詞 sort(s.begin(),s.end());//直接對string類型的字串用begin()和end()進行排序 map<string,string>::iterator it;for(it = m.begin(); it != m.end(); it++){if(it->second == s)//it->second 為值,也就是map裡第二個string的值 {cout<<it->first<<endl;//it->first 為索引索引值,也就是map裡第一個string的值 flag = 1;}}if(flag == 0)cout << "NOT A VALID WORD"<<endl; //和下面注釋掉的等效 cout << "******"<<endl;//cout << "NOT A VALID WORD\n"<<; //cout << "******\n"<<;}return 0;}