[Ybtoj Chapter 6 Example 4] String ring [String]

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here


Problem solving ideas

In this question, copy both strings and then continue to find and maintain the maximum value, so the total time complexity is greater than O (n 2) O(n^2)O ( n2 ). So you can still live


Code

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;

string a,b,x;
int ans,l1,l2;

int main(){
    
    
	cin>>a>>b;
	a=a+a,l1=a.size()-1;
	b=b+b,l2=b.size()-1;
	for(int i=0;i<l1;i++)
	{
    
    
		for(int j=1;j<=min(l1-i+1,min(l1,l2));j++)
		{
    
    
			x=a.substr(i,j);
			if(b.find(x)!=-1)
				ans=max(ans,j);
		}
	}
	printf("%d",ans);
	/*ABCEFAGADEGKABUVKLM MADJKLUVKL*/
}

Guess you like

Origin blog.csdn.net/kejin2019/article/details/113142032
Recommended