题目描述:原题链接
思路:
因为N只有100,所以从头往后一次比较,利用到了strncmp函数,第三个参数代表比较前k个的字符。
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char s[200];
int main()
{
int n;
cin>>n>>s;
for(int k = 1; k <= n; k++)
{
bool flag = true;
for (int i = 0; flag && (i + k <= n); i++)
{
for (int j = i + 1; flag && (j + k <= n); j++)
{
if (strncmp(s + i, s + j, k) == 0)
flag = false;
}
}
if (flag)
{
cout<<k<<endl;
break;
}
}
}