# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
116837 | hungcung | Lozinke (COCI17_lozinke) | C++14 | 1089 ms | 65536 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int n,dem;
string s[20005],s1,s2;
const long long mod=1e9+7,base=31;
long long pOw[15],hAsH[15];
long long getHash(long long i,long long j){
return(hAsH[j]-hAsH[i-1]*pOw[j-i+1]+mod*mod)%mod;
}
map< string,map<string,int> > m;
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
cin>>s[i];
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
int leni=s[i].length(),lenj=s[j].length(),ktra=0;
bool check=false;
if(leni<lenj){
s2=s[i];
s1=s[j];
leni=s1.length();
lenj=s2.length();
}else if(leni>=lenj){
s1=s[i];
s2=s[j];
}
if(m[s1][s2]==1){
if(leni==lenj) ans+=2;
else ans++;
}else if(m[s1][s2]==0){
pOw[0]=1ll;
s1=" "+s1;
for(int k=1;k<=leni;k++){
pOw[k]=pOw[k-1]*base%mod;
}for(int k=1;k<=leni;k++){
hAsH[k]=(hAsH[k-1]*base+s1[k]-'a'+1)%mod;
}
s2=" "+s2;
for(int k=1;k<=lenj;k++){
ktra=(ktra*base+s2[k]-'a'+1)%mod;
}for(int k=1;k<=leni-lenj+1;k++){
if(ktra==getHash(k,k+lenj-1)){
check=true;
break;
}
}
if(check==true){
if(leni==lenj) ans+=2;
else ans++;
m[s1][s2]=1;
}else if(check==false) m[s1][s2]=2;
}
}
}
printf("%d",ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |