QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#398384 | #3764. String Commutativity | xlwang# | AC ✓ | 375ms | 332068kb | C++14 | 2.1kb | 2024-04-25 11:21:01 | 2024-04-25 11:21:01 |
Judging History
answer
#include<bits/stdc++.h>
#define fr(i,j,k) for(register int i=j;i<=k;++i)
#define rf(i,j,k) for(register int i=j;i>=k;--i)
#define foredge(i,j) for(register int i=head[j];i;i=e[i].nxt)
#define pb push_back
#define Times printf("Time:%.3lf\n",clock()/CLOCKS_PER_SEC)
using namespace std;
inline int read(){
int x=0;
bool f=0;
char c=getchar();
while(!isdigit(c)) f|=(c=='-'),c=getchar();
while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
return f?-x:x;
}
inline void write(int x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}
inline void writeln(int x){write(x); puts("");}
inline void writepl(int x){write(x); putchar(' ');}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline int randfind(int l,int r){return rnd()%(r-l+1)+l;}
//inline void init(){
// int t=read();
// while(t--) work();
//}
const int Maxn=1e7+10;
int n;
int id[Maxn],ln[Maxn];
inline bool cmp(int a,int b){return ln[a]<ln[b];}
string s[Maxn];
#define ull unsigned long long
map<ull,int> mp;
const int base=19260817;
ull hsh[Maxn];
int kmp[Maxn];
char c[Maxn];
long long ans;
inline void getans(int ln){
fr(i,1,ln) hsh[i]=hsh[i-1]*base+(c[i]-'a'+1);
// fr(i,1,ln) cout<<c[i]<<' ';cout<<endl;
kmp[0]=kmp[1]=0;
int now=0;
fr(i,2,ln){
while(now && c[now+1]!=c[i]) now=kmp[now];
if(c[now+1]==c[i]) ++now;
kmp[i]=now;
}
now=ln;
while(now){
// cout<<ans<<' '<<now<<endl;
ans+=mp[hsh[now]];
now=kmp[now];
}
mp[hsh[ln]]++;
}
inline void work(){
mp.clear();
fr(i,1,n) cin>>s[i],ln[i]=s[i].size(),id[i]=i;
sort(id+1,id+n+1,cmp);
fr(i,1,n){
// cout<<"*"<<id[i]<<endl;
int num=id[i],len=s[num].size();
fr(j,1,len) c[j]=s[num][j-1];
getans(len);
}
printf("%lld\n",ans);ans=0;
}
inline void init(){
while(cin>>n) work();
}
signed main(){
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
init();
// printf("\nTIME:%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 375ms
memory: 332068kb
input:
2 a ab 2 ab ab 3 a aa aaa 100 gghj dghj ajgh djch ajgh djcjdjcj dghj djcj ggdj dghj djch dghj djch gghjgghj ajchajch djcf ajgh djcf dghj djchdjch gghj ajch ajch djch djcf dgcj djch djch djcj djch ggdjggdj ggdj ajch djcj ajch ajgh dghj ajgh ggdj dgcj ajch ajghajghajgh dgcj dacj dgcjdgcj gghj gghj ggd...
output:
0 1 3 499 544 497 508 926 864 674 954 688 787 588 798 851 985 544 599 656 594 1639 1215 849 833 574 583 652 593 1192 482 480 474 634 696 501 847 488 560 868 577 1269 736 706 478 699 774 749 501 693 517 632 579 1084 477 554 572 707 795 687 673 560 602 1266 538 675 489 580 721 635 746 700 569 914 1070...
result:
ok 2209 lines