反片语(Ananagrams,UVa156)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=835&problem=92&mosmsg=Submission+received+with+ID+18830731
主要是熟悉STL中vertor set map的用法。
感觉自己写代码越来越像STL风格了,尤其迭代器的使用,不知道这样是好是坏,有些冗长了……
#include<cstdio> #include<cstdlib> #include<iostream> #include<set> #include<map> #include<vector> #include<algorithm> using namespace std ; map<string,int> cnt ; vector<string> words ; // 重排后的单词 string repr( const string &s ) { string ans = s ; for( int i = 0 ; i < ans.length() ; i++) if( isalpha(ans[i]) ) ans[i] = tolower(ans[i]); sort(ans.begin() , ans.end()); return ans ; } int main() { string s ; cnt.clear(); while( cin >> s ) { if( s[0] == '#' ) break ; cnt[repr(s)]++; words.push_back(s); } vector<string> ans ; for( vector<string>::iterator it = words.begin() ; it != words.end() ; it++) { if( cnt[repr(*it)] == 1 ) ans.push_back(*it) ; } sort(ans.begin() , ans.end() ); for( int i = 0 ; i < ans.size() ; i++) cout << ans[i] << endl ; return 0 ; }
浙公网安备 33010602011771号