由於1<=a<=b<=12,因此能夠將輸入的字符所有存進str,而後for i:a~b,依次檢測str中長度爲i的字符串出現的次數,存進map<string,int> map1,而後將map1的key和value翻轉,存進multimap<int,string> map2,而後for i:1~n,每次取出第i大的key值對應的value,能夠存進set<string>中輸出。感受複雜度蠻高,可是最後一個測試裏僅僅0.184s,出乎意料啊。另外程序寫的極爲難看,噁心到我本身了。。。ios
最近在搞數模培訓,上午培訓,下午晚上自由活動,但是一有時間就想玩,不想玩了吧就想寫個程序,數模啊數模,我可不想這麼熱的天是在這混日子啊。。。測試
/* ID:jzzlee1 PROB:contact LANG:C++ */ //#include<iostream> #include<fstream> #include<map> #include<set> #include<string> #include<cstring> using namespace std; ifstream cin("contact.in"); ofstream cout("contact.out"); string str,s; map<string,int> map1; multimap<int,string> map2; map<string,int>::iterator iter; multimap<int,string>::reverse_iterator p,q; class rule:greater<string> { public: bool operator () (string b1,string b2) const { if(b1.size()>b2.size()) return 0; else if(b1.size()<b2.size()) return 1; else return b1<b2; } }; int main() { int a,b,n; cin>>a>>b>>n; getchar(); while(getline(cin,s)) str+=s; int i,j; if(b>str.size()) b=str.size(); if(n>str.size()) n=str.size(); for(i=a;i<=b;i++) { for(j=0;j+i<=str.size();j++) { s.assign(str,j,i); ++map1[s]; } } for(iter=map1.begin();iter!=map1.end();iter++) map2.insert(make_pair(iter->second,iter->first)); p=q=map2.rbegin(); for(i=0;i!=n;i++) { j=1; set<string,rule> iset; while(p!=map2.rend()&&p->first==q->first) { iset.insert(p->second); p++; } set<string,rule>::reverse_iterator iser=iset.rbegin(); if(iset.empty()) break; cout<<q->first<<endl; set<string,rule>::iterator iseter; for(iseter=iset.begin();iseter!=iset.end();iseter++) { if(j%6) { if(j%6!=1) cout<<" "; cout<<*iseter; } else cout<<" "<<*iseter<<endl; j++; } q=p; if((j-1)%6) cout<<endl; } return 0; }