北大的这道考研上机题不难,暴力枚举一下就好了。题目要求输出按字典序,这时候关联容器map就派上用场了,map里的数据本身就是按照键值排列的,所以只要顺序输出容器中的元素即可。
题目URL:http://ac.jobdu.com/problem.php?id=1149
我的AC代码:
#include <iostream> #include <map> #include <string> #include <string.h> #include <stdio.h> using namespace std; const int Max = 110; char str[Max]; int main() { int len; while(scanf("%s", str) != EOF) { map<string, int> m; len = strlen(str); for(int i(0); i<len; ++i) for(int j(i); j<len; ++j) { string temp(str+i, j-i+1); m[temp] ++; } map<string, int>::iterator iter; for(iter=m.begin(); iter!=m.end(); ++iter) if(iter->second > 1) cout << iter->first << ' ' << iter->second << endl; } system("pause"); return 0; }