2020 GDUT Rating Contest I (Div. 2) I. Where Am I?

来源 codeforces 2020 GDUT Personal Training Contest I (Div. 2) CF链接

题目:
Farmer John has gone out for a walk down the road and thinks he may now be lost! Along the road there are N farms (1≤N≤100) in a row. Farms unfortunately do not have house numbers, making it hard for Farmer John to figure out his location along the road. However, each farm does have a colorful mailbox along the side of the road, so Farmer John hopes that if he looks at the colors of the mailboxes nearest to him, he can uniquely determine where he is.

Each mailbox color is specified by a letter in the range A…Z, so the sequence of N mailboxes down the road can be represented by a string of length N containing letters in the range A…Z. Some mailboxes may have the same colors as other mailboxes. Farmer John wants to know what is the smallest value of K such that if he looks at any sequence of K consecutive mailboxes, he can uniquely determine the location of that sequence along the road.

For example, suppose the sequence of mailboxes along the road is ‘ABCDABC’. Farmer John cannot set K=3, since if he sees ‘ABC’, there are two possible locations along the road where this consecutive set of colors might be. The smallest value of K that works is K=4, since if he looks at any consecutive set of 4 mailboxes, this sequence of colors uniquely determines his position along the road.

Input
The first line of input contains N, and the second line contains a string of N characters, each in the range A…Z.

Output
Print a line containing a single integer, specifying the smallest value of K that solves Farmer John’s problem.

Example
input
7
ABCDABC
output
4

题意:在给定的字符串中找出最短唯一连续子序列的长度。如样例ABCDABC的答案是ABCD,长度为4.
思路:给定的字符串最长才100,故可以直接暴力枚举每个连续子串,用strncpy来选定从k位置开始的j长度的字符串为答案子串,然后判断是否符合条件。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
int n,ans=1;
char str[110],s1[110],s2[110];

int main()
{
	cin>>n;
	scanf("%s",str);
	for (int j=1;j<=n;j++)
	{
		ans=1;
		for (int i=0;i<n;i++)
		{
			strncpy(s1,str+i,j);
			for (int k=i+1;k<n;k++)
			{
				strncpy(s2,str+k,j);
				if (strcmp(s1,s2)==0)
				{
					ans=0;
					break;
				}
			}
			if (!ans) break;
		}
		if (ans)
		{
			cout<<j;
			break;
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值