답안 #714731

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
714731 2023-03-25T08:35:47 Z amirhoseinfar1385 Necklace (Subtask 1-3) (BOI19_necklace1) C++17
5 / 85
1500 ms 2368 KB
        #include<bits/stdc++.h>
        using namespace std;
        const int maxn=10000;
        int n,m;
        int base=31,mod=923478557;
        long long mp[maxn],revmp[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;
        }
         
        int main(){
        	ios::sync_with_stdio(0);
        	cin.tie(0);
        	cout.tie(0);	
        	mp[0]=1;
        	for(int i=1;i<maxn;i++){
        		mp[i]=mp[i-1]*base%mod;
        	}
        	revmp[maxn-1]=mypow(mp[maxn-1],mod-2);
        	for(int i=maxn-2;i>=0;i--){
        		revmp[i]=revmp[i+1]*base%mod;
        	}
        	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);	
        	long long unnow=0;
        	for(int i=1;i<=n;i++){
        		hashn[i]=hashn[i-1]+mp[i-1]*s[i-1]%mod;
        		hashn[i]%=mod;
        	}
        	unnow=0;
        	for(int i=n;i>0;i--){
        		revhashn[i]=revhashn[i+1]+mp[n-i]*s[i-1]%mod;
        		revhashn[i]%=mod;
        	}
        	unnow=0;
        	for(int i=1;i<=m;i++){
        		hashm[i]=hashm[i-1]+ss[i-1]*mp[i-1]%mod;
        		hashm[i]%=mod;
        	}
        	unnow=0;
        	for(int i=m;i>0;i--){
        		revhashm[i]=revhashm[i+1]+mp[m-i]*ss[i-1]%mod;
        		revhashm[i]%=mod;
        	}
        	unnow=0;
        	vector<pair<long long ,int>>allh[n+1];
        	for(int i=1;i<=n;i++){
        		for(int j=i;j<=n;j++){
        			long long fake=mod+mod+hashn[j]-hashn[i-1];
        			fake%=mod;
        			fake*=revmp[i-1];
        			fake%=mod;
        			allh[j-i+1].push_back(make_pair(fake,i));
        		//	if(i==4&&j==n){
        		//		cout<<fake<<" 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;
        			}
        			unnow=0;
        			for(int h=i;h<=j;h++){
        				long long fake=mod+mod+hashm[j]-hashm[h-1];
        				fake%=mod;
        				fake*=revmp[h-1];
        				fake+=(hashm[h]-hashm[i-1]+mod+mod)*revmp[i-1]%mod*mp[j-h+1];
        				fake%=mod;
        				int p=lower_bound(allh[j-i+1].begin(),allh[j-i+1].end(),make_pair(fake,-1))-allh[j-i+1].begin();
        				if(p!=allh[j-i+1].size()&&allh[j-i+1][p].first==fake){
        					res=max(res,make_pair(j-i+1,make_pair(i,allh[j-i+1][p].second)));
        				}
        				fake=mod+mod+revhashm[i]-revhashm[h+1];
        				fake%=mod;
        				fake*=revmp[m-h];
        				fake+=(mod+mod+revhashm[h+1]-revhashm[j+1])*revmp[m-j]%mod*mp[h-i+1]%mod;
        				fake%=mod;
        			//	if(i==3&&j==6&&h==4){
        			//		cout<<fake<<" 2"<<endl;
        			//	}
        				p=lower_bound(allh[j-i+1].begin(),allh[j-i+1].end(),make_pair(fake,-1))-allh[j-i+1].begin();
        				if(p!=allh[j-i+1].size()&&allh[j-i+1][p].first==fake){
        					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:91:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |             if(p!=allh[j-i+1].size()&&allh[j-i+1][p].first==fake){
      |                ~^~~~~~~~~~~~~~~~~~~~
necklace.cpp:103:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |             if(p!=allh[j-i+1].size()&&allh[j-i+1][p].first==fake){
      |                ~^~~~~~~~~~~~~~~~~~~~
necklace.cpp:38:20: warning: variable 'unnow' set but not used [-Wunused-but-set-variable]
   38 |          long long unnow=0;
      |                    ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Partially correct 6 ms 624 KB Output is partially correct
2 Partially correct 6 ms 624 KB Output is partially correct
3 Partially correct 17 ms 584 KB Output is partially correct
4 Correct 12 ms 620 KB Output is correct
5 Partially correct 23 ms 596 KB Output is partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 6 ms 624 KB Output is partially correct
2 Partially correct 6 ms 624 KB Output is partially correct
3 Partially correct 17 ms 584 KB Output is partially correct
4 Correct 12 ms 620 KB Output is correct
5 Partially correct 23 ms 596 KB Output is partially correct
6 Partially correct 499 ms 2316 KB Output is partially correct
7 Partially correct 750 ms 2368 KB Output is partially correct
8 Correct 894 ms 2260 KB Output is correct
9 Execution timed out 1567 ms 2132 KB Time limit exceeded
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Partially correct 6 ms 624 KB Output is partially correct
2 Partially correct 6 ms 624 KB Output is partially correct
3 Partially correct 17 ms 584 KB Output is partially correct
4 Correct 12 ms 620 KB Output is correct
5 Partially correct 23 ms 596 KB Output is partially correct
6 Partially correct 499 ms 2316 KB Output is partially correct
7 Partially correct 750 ms 2368 KB Output is partially correct
8 Correct 894 ms 2260 KB Output is correct
9 Execution timed out 1567 ms 2132 KB Time limit exceeded
10 Halted 0 ms 0 KB -