Where Am I?(字符串)

题目


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


题意

Farmer John 出门沿着马路散步,但是他现在发现可能迷路了!

沿路有一排共 N 个农场(1≤N≤100)。不幸的是农场并没有编号,这使得 Farmer John 难以分辨他在这条路上所处的位置。然而,每个农场都沿路设有一个彩色的邮箱,所以 Farmer John 希望能够通过查看最近的几个邮箱的颜色来唯一确定他所在的位置。

每个邮箱的颜色用 A..Z 之间的一个字母来指定,所以沿着道路的 N 个邮箱的序列可以用一个长为 N 的由字母 A..Z 组成的字符串来表示。某些邮箱可能会有相同的颜色。Farmer John 想要知道最小的 K 的值,使得他查看任意连续 K 个邮箱序列,他都可以唯一确定这一序列在道路上的位置。

例如,假设沿路的邮箱序列为 "ABCDABC"。Farmer John 不能令 K=3,因为如果他看到了 "ABC",沿路有两个这一连续颜色序列可能所在的位置。最小可行的 K 的值为 K=4,因为如果他查看任意连续 4 个邮箱,这一颜色序列可以唯一确定他在道路上的位置。

【输入格式】

输入的第一行包含 N,第二行包含一个由 N 个字符组成的字符串,每个字符均在 A..Z 之内。

【输出格式】

输出一行,包含一个整数,为可以解决 Farmer John 的问题的最小 K 值。

【输入输出样例】

输入

7
ABCDABC

输出

4

思路

用i和j枚举开头,用substr来截取字符串。


AC代码

#include<bits/stdc++.h>
using namespace std;

string s;
int n, cnt;
bool flag;

int main()
{
	cin >> n >> s;
	for(int k = 1; k <= n; k++)
	{
		flag = 1;
		for(int i = 0; i + k - 1 < n; i++)
		{
			for(int j = i + 1; j + k - 1 < n; j++)
			{
				if(s.substr(i, k) == s.substr(j, k))
				{
					flag = 0;
				}
			}
		}
		if(flag)
		{
			cout << k << endl;
			break;
		}
	}
	
	return 0;
}

  • 37
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值