1. 程式人生 > >UVA 156(STL_G題)解題報告

UVA 156(STL_G題)解題報告

etl show ret return problem open main span 輸出

題目鏈接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=92

----------------------------------------------------------------------------------------------------------------------------------------------------------------

題意:對於一段文字,其中有些詞語是亂序的,要求輸出只出現一次的單詞,按字典序。

思路:對於每種字符串,保存後將其轉為小寫,在按字典序排序。如果兩個不同字符串按照上述操作進行後為相同的字符串,認為該兩個字符串為相同字符串。同時需要保存排序前和排序後的結果,便於之後查照刪除,於是采用了map的數據結構。

代碼:

技術分享圖片
#include<cstdio>
#include<sstream> 
#include<algorithm>
#include<set>
#include<iostream>
#include<string>
#include<map>
using namespace std;
set<string> s2; set<string>s1; map<string,string>m1; map<string,string>::iterator iter; int main(void){ string cur ="0"; string s ="0"; while(getline(cin,s)){ if (s=="#") break; stringstream input1(s); while(input1>>cur){
string cs = cur; for(int i=0;i<cur.size();i++){ cur[i]=tolower(cur[i]); } sort(cur.begin(),cur.end()); m1.insert(pair<string, string>(cur,cs)); if(s1.find(cur)==s1.end()){ s2.insert(cs); s1.insert(cur); }else{ iter = m1.find(cur); if(iter!=m1.end()) { s2.erase(iter->second); } } } } set<string>::iterator it; for(it=s2.begin();it!=s2.end();it++) cout<<*it<<endl; return 0; }
View Code

UVA 156(STL_G題)解題報告