【NOIP模拟赛】最大跨距

【NOIP模拟赛】最大跨距_第1张图片

hash加贪心就行了

#include
#define M 10007
#define MOD 1000000007
using namespace std;
long long s[100005],s1,s2;
long long basic1,basic2;
long long tot,tot1,tot2,st=-1;
char ch;
int main(){
   basic1=basic2=1;
   ch=getchar();
   while(ch!=','){
   	s[++tot]=s[tot-1]*M%MOD+ch%MOD;
   	s[tot]%=MOD;
   	ch=getchar();
   }
   ch=getchar();
   while(ch!=','){
   	tot1++;
   	s1*=M;
   	s1%=MOD;
   	basic1*=M;
   	basic1%=MOD;
   	s1+=ch;
   	ch=getchar();
   }
   ch=getchar();
   while(ch!=10){
   	tot2++;
   	s2*=M;
   	s2%=MOD;
   	basic2*=M;
   	basic2%=MOD;
   	s2+=ch;
   	ch=getchar();
   }
   s1%=MOD;
   s2%=MOD;
   for(int i=0;i<=tot-tot1;i++){
   	long long calc=s[tot1+i]-(s[i]%MOD)*basic1%MOD;
   	while(calc<0) calc+=MOD;
   	if(calc==s1){
   		st=i+tot1;
   		break;
   	}
   }
   if(st==-1){
   	puts("-1");
   	return 0;
   }
   for(int i=tot-tot2;i>=st;i--){
   	long long calc=s[tot2+i]-(s[i]%MOD)*basic2%MOD;
   	while(calc<0) calc+=MOD;
   	if(calc==s2){
   		printf("%d",i-st);
   		return 0;
   	}
   }
   puts("-1");
   return 0;
}

你可能感兴趣的:(提高组,NOIP,模拟赛,最大跨距)