[Ybtoj high-efficiency advanced 2.1] D. String ring [String]

Insert picture description here

analysis

There is no need to copy the string, just mod the length of the string and make a judgment directly.

Upload code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

string a,b;
int mn,ans,mx;

int main()
{
    
    
	cin>>a>>b;
	mn=min(a.length(),b.length());
	for(int i=0;i<=a.length()-1;i++)
	{
    
    
		for(int j=0;j<=b.length()-1;j++)
		{
    
    
			int k=i,f=j;
			ans=0;
			while(a[k]==b[f])
			{
    
    
				ans++;
				k=(k+1)%a.length();
				f=(f+1)%b.length();
				if(ans>mn) break;
			}
			mx=max(ans,mx);
		}
	}
	cout<<mx;
	return 0;
}

Guess you like

Origin blog.csdn.net/dglyr/article/details/113349627