SSL

xiaoxiao2021-02-28  41

题意:

    求在一个字符串中里出现了至少2次的子串的最大长度。

思路:

    三重循环枚举。

代码:

#include<cstdio> #include<string> using namespace std; char c[121],l; int pd(int x,int x1,int y)//判断x开头和x1开头长度为y的子串是否相等 { int a=x1,b=x; if (c[a]!=c[b]) return false; while (y>=0) { y--; if (c[a]!=c[b]) return false; a++;b++; } return true; } int main() { scanf("%s",c); l=strlen(c); for (int i=l-1;i>=1;i--)//枚举长度 for (int j=0;j<=l-i+1;j++)//枚举第一个的开头 for (int k=0;k<=l-i+1;k++)//第二个的开头 if (j!=k&&pd(j,k,i)) {printf("%d",i+1);return 0;} printf("0"); }
转载请注明原文地址: https://www.6miu.com/read-2628166.html

最新回复(0)