HDU-3374 String Problem

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/C_13579/article/details/85860720

地址:http://acm.hdu.edu.cn/showproblem.php?pid=3374

思路:最小表示法模板题

Code:

#include<iostream>
#include<cmath>
using namespace std;

int n;
string str;

int main()
{
	ios::sync_with_stdio(false);
	while(cin>>str){
		n=str.length();
		str+=str;
		int res1=1,res2=1,s1=1,s2=1;
		int l=0,r=1,t;
		while(l<n&&r<n){
			for(t=0;t<n;++t)
				if(str[l+t]!=str[r+t])	break;
			if(t==n){
				if(l>r)	swap(l,r);
				res1=l+1;	s1=n/(r-l);	break;
			}else{
				if(str[l+t]<str[r+t]){
					r+=t+1;
					res1=l+1;	s1=1;
				}else{
					l+=t+1;
					res1=r+1;	s1=1;
				}
				if(l==r)	++r;
			}
		}
		l=0;	r=1;
		while(l<n&&r<n){
			for(t=0;t<n;++t)
				if(str[l+t]!=str[r+t])	break;
			if(t==n){
				if(l>r)	swap(l,r);
				res2=l+1;	s2=n/(r-l);	break;
			}else{
				if(str[l+t]>str[r+t]){
					r+=t+1;
					res2=l+1;	s2=1;
				}else{
					l+=t+1;
					res2=r+1;	s2=1;
				}
				if(l==r)	++r; 
			}
		}
		cout<<res1<<" "<<s1<<" "<<res2<<" "<<s2<<endl;
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/C_13579/article/details/85860720