#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
lozinke.cpp: In function 'int main()':
lozinke.cpp:12:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
1024 KB |
Output is correct |
3 |
Correct |
9 ms |
1536 KB |
Output is correct |
4 |
Correct |
124 ms |
6748 KB |
Output is correct |
5 |
Correct |
932 ms |
47544 KB |
Output is correct |
6 |
Execution timed out |
1063 ms |
65536 KB |
Time limit exceeded |
7 |
Execution timed out |
1056 ms |
62316 KB |
Time limit exceeded |
8 |
Execution timed out |
1084 ms |
64760 KB |
Time limit exceeded |
9 |
Execution timed out |
1089 ms |
55592 KB |
Time limit exceeded |
10 |
Execution timed out |
1076 ms |
58436 KB |
Time limit exceeded |
11 |
Execution timed out |
1051 ms |
51824 KB |
Time limit exceeded |
12 |
Execution timed out |
1089 ms |
57708 KB |
Time limit exceeded |
13 |
Execution timed out |
1050 ms |
51624 KB |
Time limit exceeded |
14 |
Execution timed out |
1046 ms |
61788 KB |
Time limit exceeded |
15 |
Execution timed out |
1072 ms |
55924 KB |
Time limit exceeded |
16 |
Execution timed out |
1056 ms |
12576 KB |
Time limit exceeded |
17 |
Execution timed out |
1070 ms |
1024 KB |
Time limit exceeded |
18 |
Execution timed out |
1075 ms |
1024 KB |
Time limit exceeded |
19 |
Execution timed out |
1075 ms |
50192 KB |
Time limit exceeded |
20 |
Execution timed out |
1069 ms |
12820 KB |
Time limit exceeded |