답안 #714747

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
714747 2023-03-25T09:00:56 Z amirhoseinfar1385 Necklace (Subtask 1-3) (BOI19_necklace1) C++17
5 / 85
1500 ms 1900 KB
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=10000;
    int n,m;
    int base=29,mod=923478557,base2=31,mod2=1e9+7;
    int ww(int a){
    	if(a>=mod){
    		a-=mod;
    	}
    	return a;
    }
    int ww2(int a){
    	if(a>=mod2){
    		a-=mod2;
    	}
    	return a;
    }
    int mp[maxn],revmp[maxn],mp2[maxn],revmp2[maxn];
    long long mypow(long long m,long long y){
    	if(y==0){
    		return 1;
    	}
    	long long p=mypow(m,(y>>1));
    	p*=p;
    	p%=mod;
    	if(y&1){
    		p*=m;
    		p%=mod;
    	}
    	return p;
    }
    long long mypow2(long long m,long long y){
    	if(y==0){
    		return 1;
    	}
    	long long p=mypow(m,(y>>1));
    	p*=p;
    	p%=mod2;
    	if(y&1){
    		p*=m;
    		p%=mod2;
    	}
    	return p;
    } 
    int main(){
    	ios::sync_with_stdio(0);
    	cin.tie(0);
    	cout.tie(0);	
    	mp[0]=1;
    	mp2[0]=1;
    	for(int i=1;i<maxn;i++){
    		mp[i]=1ll*mp[i-1]*base%mod;
    		mp2[i]=1ll*mp2[i-1]*base2%mod2;
    	}
    	revmp[maxn-1]=mypow(mp[maxn-1],mod-2);
    	revmp2[maxn-1]=mypow2(mp2[maxn-1],mod2-2);
    	for(int i=maxn-2;i>=0;i--){
    		revmp[i]=1ll*revmp[i+1]*base%mod;
    		revmp2[i]=1ll*revmp2[i+1]*base2%mod2;
    	}
    	string s,ss;
    	cin>>s>>ss;
    	n=s.size();
    	m=ss.size();
    	vector<int>hashn(n+2),revhashn(n+2),hashm(m+2),revhashm(m+2);	
    	vector<int>hashn2(n+2),revhashn2(n+2),hashm2(m+2),revhashm2(m+2);	
    	for(int i=1;i<=n;i++){
    		hashn[i]=(1ll*mp[i-1]*s[i-1]+hashn[i-1])%mod;
    		hashn2[i]=(1ll*mp2[i-1]*s[i-1]+hashn2[i-1])%mod2;
    	}
    	for(int i=n;i>0;i--){
    		revhashn[i]=(1ll*mp[n-i]*s[i-1]+revhashn[i+1])%mod;
    		revhashn2[i]=(1ll*mp2[n-i]*s[i-1]+revhashn2[i+1])%mod2;
    	}
    	for(int i=1;i<=m;i++){
    		hashm[i]=(1ll*ss[i-1]*mp[i-1]+hashm[i-1])%mod;
    		hashm2[i]=(1ll*ss[i-1]*mp2[i-1]+hashm2[i-1])%mod2;
    	}
    	for(int i=m;i>0;i--){
    		revhashm[i]=(1ll*mp[m-i]*ss[i-1]+revhashm[i+1])%mod;
    		revhashm2[i]=(1ll*mp2[m-i]*ss[i-1]+revhashm2[i+1])%mod2;
    	}
    	vector<pair<pair<int,int>,int>>allh[n+1];
    	for(int i=1;i<=n;i++){
    		for(int j=i;j<=n;j++){
    			int fake=mod+hashn[j]-hashn[i-1];
    			int fake2=mod2+hashn2[j]-hashn2[i-1];
    			fake=ww(fake);
    			fake2=ww2(fake2);
    			fake2=1ll*fake2*revmp2[i-1]%mod2;
    			fake=1ll*fake*revmp[i-1]%mod;
    			allh[j-i+1].push_back(make_pair(make_pair(fake,fake2),i));
    	//		if(i==4&&j==n){
    	//			cout<<fake2<<" 1"<<endl;
    	//		}
    		}
    	}
    	for(int i=0;i<=n;i++){
    		sort(allh[i].begin(),allh[i].end());
    		allh[i].resize(unique(allh[i].begin(),allh[i].end())-allh[i].begin());
    	}
    	pair<int,pair<int,int>>res;
    	res.first=-1;
    	for(int i=1;i<=m;i++){
    		for(int j=i;j<=m;j++){
    			if(j-i+1>n||j-i+1<=res.first){
    				continue;
    			}
    			for(int h=i;h<=j;h++){
    				int fake=mod+hashm[j]-hashm[h-1];
    				int fake2=mod2+hashm2[j]-hashm2[h-1];
    				fake=ww(fake);
    				fake2=ww2(fake2);
    				fake=1ll*fake*revmp[h-1]%mod;
    				fake2=1ll*fake2*revmp2[h-1]%mod2;
    				fake+=1ll*(hashm[h]-hashm[i-1]+mod)*revmp[i-1]%mod*mp[j-h+1]%mod;
    				fake2+=1ll*(hashm2[h]-hashm2[i-1]+mod2)*revmp2[i-1]%mod2*mp2[j-h+1]%mod2;
    				fake=ww(fake);
    				fake2=ww(fake2);
    				int p=lower_bound(allh[j-i+1].begin(),allh[j-i+1].end(),make_pair(make_pair(fake,fake2),-1))-allh[j-i+1].begin();
    				if(p!=allh[j-i+1].size()&&allh[j-i+1][p].first==make_pair(fake,fake2)){
    					res=max(res,make_pair(j-i+1,make_pair(i,allh[j-i+1][p].second)));
    				}
    				fake=mod+revhashm[i]-revhashm[h+1];
    				fake2=mod2+revhashm2[i]-revhashm2[h+1];
    //				if(i==3&&j==6&&h==4){
    //					cout<<fake<<" 2 "<<endl;
    //				}
    				fake=ww(fake);
    				fake2=ww2(fake2);
    				fake=1ll*fake*revmp[m-h]%mod;
    				fake2=1ll*fake2*revmp2[m-h]%mod2;
    				int z=1ll*(mod+revhashm[h+1]-revhashm[j+1])*revmp[m-j]%mod*mp[h-i+1]%mod;
    				int z2=1ll*(mod2+revhashm2[h+1]-revhashm2[j+1])*revmp2[m-j]%mod2*mp2[h-i+1]%mod2;
    				fake+=z;
    				fake2+=z2;
    				fake2=ww(fake2);
    				fake=ww(fake);
    	//			if(i==3&&j==6&&h==4){
    	//				cout<<fake<<" 2 "<<z<<endl;
    	//			}
    				p=lower_bound(allh[j-i+1].begin(),allh[j-i+1].end(),make_pair(make_pair(fake,fake2),-1))-allh[j-i+1].begin();
    				if(p!=allh[j-i+1].size()&&allh[j-i+1][p].first==make_pair(fake,fake2)){
    					res=max(res,make_pair(j-i+1,make_pair(i,allh[j-i+1][p].second)));
    				}
    			}
    		}
    	}
    	if(res.first==-1){
    		cout<<-1<<"\n";
    		return 0;
    	}
    	cout<<res.first<<"\n";
    	cout<<res.second.second-1<<" "<<res.second.first-1<<"\n";
    }

Compilation message

necklace.cpp: In function 'int main()':
necklace.cpp:121:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |         if(p!=allh[j-i+1].size()&&allh[j-i+1][p].first==make_pair(fake,fake2)){
      |            ~^~~~~~~~~~~~~~~~~~~~
necklace.cpp:143:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  143 |         if(p!=allh[j-i+1].size()&&allh[j-i+1][p].first==make_pair(fake,fake2)){
      |            ~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Partially correct 17 ms 556 KB Output is partially correct
4 Correct 13 ms 596 KB Output is correct
5 Partially correct 26 ms 468 KB Output is partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Partially correct 17 ms 556 KB Output is partially correct
4 Correct 13 ms 596 KB Output is correct
5 Partially correct 26 ms 468 KB Output is partially correct
6 Partially correct 451 ms 1900 KB Output is partially correct
7 Partially correct 780 ms 1900 KB Output is partially correct
8 Partially correct 916 ms 1876 KB Output is partially correct
9 Execution timed out 1547 ms 1748 KB Time limit exceeded
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Partially correct 17 ms 556 KB Output is partially correct
4 Correct 13 ms 596 KB Output is correct
5 Partially correct 26 ms 468 KB Output is partially correct
6 Partially correct 451 ms 1900 KB Output is partially correct
7 Partially correct 780 ms 1900 KB Output is partially correct
8 Partially correct 916 ms 1876 KB Output is partially correct
9 Execution timed out 1547 ms 1748 KB Time limit exceeded
10 Halted 0 ms 0 KB -