QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#540015 | #3764. String Commutativity | abovecloud# | AC ✓ | 379ms | 19968kb | C++14 | 1.1kb | 2024-08-31 16:15:07 | 2024-08-31 16:15:09 |
Judging History
answer
/*Time:2024-08-31 15:10:25*/
#include <bits/stdc++.h>
#define pii pair<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define int long long
using namespace std;
const int MOD = 998244353;
const int MAXN = 100005;
int n;
int base=131;
map<int,int> mp;
string vs[MAXN];
void solve() {
// int n,m
mp.clear();
int ans=0;
for(int i=0;i<n;i++){
cin>>vs[i];
}
sort(vs,vs+n,[&](string& x,string& y){
return x.size()<y.size();
});
for(int i=0;i<n;i++){
string s= vs[i];
// cout<<s<<endl;
vi a;
int h1=0,h2=0,h3=0;
int len=s.size();
for(int i=0;i<len;i++){
a.push_back(s[i]-'a'+1);
h1=(h1*base%MOD+a.back())%MOD;
}
int b2=1;
int t=0;
for(int i=0;i<len;i++){
h2=(h2*base%MOD+a[i])%MOD;
h3=(a[len-1-i]*b2%MOD+h3)%MOD;
b2*=base;
b2%=MOD;
// cout<<h2<<" "<<h3<<endl;
if(h2==h3){
t+=mp[h2];
// mp[h2]++;
// break;
}
}
ans+=t;
// cout<<ans<<endl;
// ans
mp[h1]++;
}
cout<<ans<<endl;
}
int32_t main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T = 1;
// cin >> T;
while (cin>>n)solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 379ms
memory: 19968kb
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