CodeForces - 23A You're Given a String...

题目
由于题目中要求该字符子串出现两次所以直接从头开始进行匹配,第一个for代表该字符子串第一次出现,第二个for代表该字符子串第二次出现。通过max记录最大长度。

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main( )
{
	string s;
	cin>>s;
	int len=s.size();
	int maxx=0;
	for(int i=0;i<len;i++)
	{
		for(int j=i+1;j<len;j++)
		{
			int m=0;
			while(s[i+m]==s[j+m])
			m++;	
			maxx=max(m,maxx);
		}
		
	}
	cout<<maxx<<endl;
	return 0;
}
发布了21 篇原创文章 · 获赞 1 · 访问量 298

猜你喜欢

转载自blog.csdn.net/qq_44722533/article/details/103651593