QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#385356 | #3764. String Commutativity | 秦始皇派蒙恬还原神舟十二 (Jiancong Wen, Chu Jin, Zekai Zhang) | AC ✓ | 115ms | 6596kb | C++17 | 1.4kb | 2024-04-10 17:58:36 | 2024-04-10 17:58:37 |
Judging History
answer
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <bitset>
#include <cstdio>
#include <string>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define lowbit(x) x&(-x)
#define pi 3.14159265358
#define lc k<<1;
#define rc k<<1|1
#define ppb push_back
#define mod 1000000007
#define fori(i,l,r) for(int i=l;i<=r;i++)
#define forj(j,l,r) for(int j=l;j<=r;j++)
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair <int, int> pll;
const int N = 2e6 + 10;
const int P = 131;
ll n, m, ans = 0;
map<string, ll>mp;
ULL ha[N];
ULL p[N];
int main() {
ios::sync_with_stdio(0);
cin.tie();
cout.tie();
string s;
p[0] = 1;
while (cin >> n) {
mp.clear();
for (int i = 1; i <= n; i++){
cin >> s;
ll ls = s.size();
s = " " + s;
for (int i = 1; i<=ls; i++){
p[i] = p[i-1] * P;
ha[i] = ha[i-1] * P+s[i]-'a'+1;
}int hh;
for (int i = 1; i <= n; i++){
int f = 1;
if (ls%i == 0){
ll k = ha[i];
for (int j = i; j <= ls; j += i){
ll kk = ha[j] - ha[j - i] * p[i];
if (k != kk){
f = 0;
break;
}
}if (f){
hh = i;
break;
}
}
}string cc = s.substr(1, hh);
mp[cc]++;
}ll ans= 0;
for (auto c : mp){
ll cf = c.second;
ans += cf*(cf - 1) / 2;
}cout << ans << "\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 115ms
memory: 6596kb
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