Submission #602704

# Submission time Handle Problem Language Result Execution time Memory
602704 2022-07-23T10:24:25 Z WongChun1234 Necklace (Subtask 1-3) (BOI19_necklace1) C++14
0 / 85
142 ms 14520 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=3499999991;
int n,m,ans,l,r;
ll hsh[2][3050],prec[3050];
string a,b;
map<ll,int> hv;
void computehash(int id,string str){
	for (int i=1;i<str.length();i++){
		hsh[id][i]=hsh[id][i-1]*27+str[i]-'a'+1;
		hsh[id][i]%=MOD;
	}
}
ll gethash(int id,int l,int r){
	//(]
	return ((hsh[id][r]-hsh[id][l]*prec[r-l])%MOD+MOD)%MOD;
}
int main(){
	cin>>a>>b;
	a=' '+a;
	b=' '+b;
	computehash(0,a);
	computehash(1,b);
	prec[0]=1;
	for (int i=1;i<=3000;i++) prec[i]=prec[i-1]*27%MOD;
	for (int i=1;i<a.length();i++){
		for (int j=i;j<a.length();j++){
			for (int k=0;k<i;k++){
				//cout<<i<<"-"<<j<<"--"<<k<<"::"<<(gethash(0,i-1,j)*prec[i-k-1]+gethash(0,k,i-1))%MOD<<"\n";
				hv[(gethash(0,i-1,j)*prec[i-k-1]+gethash(0,k,i-1))%MOD]=k+1;
			}
		}
	}
	for (int i=1;i<b.length();i++){
		for (int j=i;j<b.length();j++){
			for (int k=0;k<i;k++){
				//cout<<i<<"-"<<j<<"--"<<k<<"::"<<(gethash(1,i-1,j)*prec[i-k-1]+gethash(1,k,i-1))%MOD<<"\n";
				if (!hv[(gethash(1,i-1,j)*prec[i-k-1]+gethash(1,k,i-1))%MOD]) continue;
				if (j-k<ans) continue;
				ans=j-k;
				l=hv[(gethash(1,i-1,j)*prec[i-k-1]+gethash(1,k,i-1))%MOD]-1;
				r=k;
			}
		}
	}
	reverse(b.begin()+1,b.end());
	computehash(1,b);
	for (int i=1;i<b.length();i++){
		for (int j=i;j<b.length();j++){
			for (int k=0;k<i;k++){
				//cout<<i<<"-"<<j<<"--"<<k<<"::"<<(gethash(1,i-1,j)*prec[i-k-1]+gethash(1,k,i-1))%MOD<<"\n";
				if (!hv[(gethash(1,i-1,j)*prec[i-k-1]+gethash(1,k,i-1))%MOD]) continue;
				if (j-k<ans) continue;
				ans=j-k;
				l=hv[(gethash(1,i-1,j)*prec[i-k-1]+gethash(1,k,i-1))%MOD]-1;
				r=b.length()-1-j;
			}
		}
	}
	cout<<ans<<"\n"<<l<<" "<<r<<"\n";
}

Compilation message

necklace.cpp: In function 'void computehash(int, std::string)':
necklace.cpp:10:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 |  for (int i=1;i<str.length();i++){
      |               ~^~~~~~~~~~~~~
necklace.cpp: In function 'int main()':
necklace.cpp:27:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |  for (int i=1;i<a.length();i++){
      |               ~^~~~~~~~~~~
necklace.cpp:28:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |   for (int j=i;j<a.length();j++){
      |                ~^~~~~~~~~~~
necklace.cpp:35:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |  for (int i=1;i<b.length();i++){
      |               ~^~~~~~~~~~~
necklace.cpp:36:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |   for (int j=i;j<b.length();j++){
      |                ~^~~~~~~~~~~
necklace.cpp:49:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |  for (int i=1;i<b.length();i++){
      |               ~^~~~~~~~~~~
necklace.cpp:50:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |   for (int j=i;j<b.length();j++){
      |                ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 106 ms 11076 KB Output is correct
2 Correct 142 ms 12104 KB Output is correct
3 Correct 82 ms 9076 KB Output is correct
4 Incorrect 130 ms 14520 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 106 ms 11076 KB Output is correct
2 Correct 142 ms 12104 KB Output is correct
3 Correct 82 ms 9076 KB Output is correct
4 Incorrect 130 ms 14520 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 106 ms 11076 KB Output is correct
2 Correct 142 ms 12104 KB Output is correct
3 Correct 82 ms 9076 KB Output is correct
4 Incorrect 130 ms 14520 KB Output isn't correct
5 Halted 0 ms 0 KB -