璟雯院

珺璟如晔,雯华若锦

用户工具

站点工具


icpc:problems:子串计算
problems
名称子串计算
题目编号2.1.2472
题目链接noi.openjudge.cn/…
来源OpenJudge
算法分类入门_字符串
难易程度容易

子串计算

想法

字符串比较+排序

代码实现

#include<cstdio>
#include<cstring>
#include<algorithm>
 
struct node{
	char str[100];
	int num;
};
char line[100];
node tong[100010];
int tong_cnt;
 
bool check(int a, int b, int t)
{
	if(b-a+1 != (int)strlen(tong[t].str))
		return false;
 
	for(int i=0; i<(int)strlen(tong[t].str); ++i)
		if(tong[t].str[i] != line[a+i])
			return false;
 
	return true;
}
 
bool cmp(node a, node b)
{
	return strcmp(a.str,b.str) < 0;
}
 
int main()
{
	scanf("%s", line);
	int len = strlen(line);
 
	for(int i=0; i<len; ++i)
	{
		for(int j=i; j<len; ++j)
		{
			bool flag = true;
			for(int t = 1; t <= tong_cnt; ++t)
				if(check(i,j,t))
				{
					tong[t].num++;
					flag = false;
					break;
				}
 
			if(flag)
			{
				tong_cnt++;
				for(int t=i; t<=j; ++t)
					tong[tong_cnt].str[t-i] = line[t];
				tong[tong_cnt].str[j-i+1] = '\0';
				tong[tong_cnt].num=1;
			}
		}
	}
 
	std::sort(tong + 1, tong + 1 + tong_cnt, cmp);
 
	for(int i=1; i<=tong_cnt; ++i)
	{
		if(tong[i].num > 1)
			printf("%s %d\n", tong[i].str, tong[i].num);
	}
 
	return 0;
}
/app/www/public/data/pages/icpc/problems/子串计算.txt · 最后更改: 2023/09/30 13:40 由 温婕莺