现在的位置: 首页 > 综合 > 正文

九度OJ 1149:子串计算 暴力枚举

2018年05月25日 ⁄ 综合 ⁄ 共 591字 ⁄ 字号 评论关闭

    北大的这道考研上机题不难,暴力枚举一下就好了。题目要求输出按字典序,这时候关联容器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;
}

抱歉!评论已关闭.