比赛
题目
求一个字符串中出现至少两次的最长子串的长度
分析
纯模拟,O( n 2 n^2 n2),碰到相同的字母往后找。
佩服哈希%_%
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[101]; int len,ans;
int main(){
gets(s); len=strlen(s);
for (int i=0;i<len;i++)
for (int j=i+1;j<len;j++)
if (s[i]==s[j]){//找到了
int sum=0,x=i,y=j;
while (s[x]==s[y]&&x<strlen(s)&&y<strlen(s)) x++,y++,sum++;//往后找
ans=max(sum,ans);//最长的长度
}
printf("%d",ans);
return 0;
}