- 题目描述
有两个由字符构成的环。
请写一个程序,计算这两个字符环上最长连续公共字符串的长度。
例如,字符串 ABCEFAGADEGKABUVKLM 的首尾连在一起,构成一个环;字符串 MADJKLUVKL 的首尾连在一起,构成另一个环;UVKLMA 是这两个环的一个连续公共字符串。- 输入格式
一行,包含两个字符串,分别对应一个字符环。这两个字符串之间用单个空格分开。字符串长度不超过 ,且不包含空格等空白符。- 输出格式
输出一个整数,表示这两个字符环上最长公共字符串的长度。- 样例输入
ABCEFAGADEGKABUVKLM MADJKLUVKL- 样例输出
6
新操作:
str.substr(pos, lengh);
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
string s, c;
int maxn = -1;
int main() {
cin >> s >> c;
if(s.length() < c.length()) swap(s, c);
s = s + s;
c = c + c;
for(int i = 0; i < c.length() / 2; i ++)
for(int j = 1; j <= c.length() / 2; j ++) {
int b = s.find(c.substr(i, j));//又是新的操作!
// printf("%d \n", b);//冷知识:搜不到会自动返回-1,太好啦!
if(~b) maxn = max(maxn, j);
}
cout << maxn;
return 0;
}