QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#959401 | #3764. String Commutativity | xianjing# | AC ✓ | 397ms | 23580kb | C++23 | 6.1kb | 2025-03-31 18:56:03 | 2025-03-31 18:56:07 |
Judging History
answer
//.................................................................................
//..................##..............##..................###....#####....####.......
//..................##..............##.................#####..##...##..#####.......
//..................##..............##................###.###.##...##..##..........
//......##..###..##.##.##...#####...##....####........##...##.##...##.##.###.......
//......##..###..##.######..######..##...######.......##...##..#####..#######......
//.......#..###..##.##..##......##..##..##...##.......##...##..#####..##...##......
//.......####.####..##..##..######..##..#######.####..##...##.##...##.##...##......
//.......####.####..##..##.##...##..##..##............###.###.##...##.##...##......
//........##..####..##..##.#######..##..######.........#####..##...##..#####.......
//........##...##...##..##..###.##..##...#####..........###....#####....###........
//.................................................................................
/**
* 2025-03-31 17:12:33
**/
//#pragma GCC optimize(1, 2, 3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define all1(x) x.begin() + 1, x.end()
#define sort_all(x) sort(all(x))
#define sort1_all(x) sort(all1(x))
#define reverse_all(x) reverse(all(x))
#define reverse1_all(x) reverse(all1(x))
using i128 = __int128;
using ll=long long;
#define ldb long double
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ldb, ldb> pdd;
typedef pair<ll, int> pli;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<string, ll> psl;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef map<int, int> mii;
typedef map<int, bool> mib;
typedef map<string, int> msi;
#define RED cout << "\033[91m"
#define GREEN cout << "\033[92m"
#define YELLOW cout << "\033[93m"
#define BLUE cout << "\033[94m"
#define MAGENTA cout << "\033[95m"
#define CYAN cout << "\033[96m"
#define RESET cout << "\033[0m"
// 红色
#define DEBUG1(x) \
RED; \
cout << #x << " : " << x << endl; \
RESET;
// 绿色
#define DEBUG2(x) \
GREEN; \
cout << #x << " : " << x << endl; \
RESET;
// 蓝色
#define DEBUG3(x) \
BLUE; \
cout << #x << " : " << x << endl; \
RESET;
// 品红
#define DEBUG4(x) \
MAGENTA; \
cout << #x << " : " << x << endl; \
RESET;
// 青色
#define DEBUG5(x) \
CYAN; \
cout << #x << " : " << x << endl; \
RESET;
// 黄色
#define DEBUG6(x) \
YELLOW; \
cout << #x << " : " << x << endl; \
RESET;
#define dddmin(x,y) (x=min(x,y))
#define dddmax(x,y) (x=max(x,y))
//const int mod=1e9+7;
//const int mod=998244353;
random_device RD;
mt19937_64 rd(RD());
ll rn(int l, int r){
return l + rd() % (r - l + 1);
}
using a2 = array<int, 2>;
const int base = 131;
const a2 mod = {998244353, 1000000007};
a2 operator%(a2 x, a2 y){
x[0] %= y[0];
x[1] %= y[1];
return x;
}
a2 qpow(a2 a,int b){
a2 base={1, 1};
while(b){
if(b&1)base=a2{base[0] * a[0], base[1] * a[1]}%mod;
b>>=1;
a=a2{a[0] * a[0], a[1] * a[1]}%mod;
}
return base;
}
int qpow(int a,int b, int mod){
int base=1;
while(b){
if(b&1)base=base*a%mod;
b>>=1;
a=a*a%mod;
}
return base;
}
inline int lowbit(int x){
return x&-x;
}
inline int lt(int x){
return 1ll<<x;
}
//struct node{
// signed ch[26];
// signed val;
// node(){
// for(int i = 0; i < 26; i ++)
// ch[i] = 0;
// val = 0;
// }
//}d[5000005];
//int cnt;
//void insert(string &s, vi &p){
// //p end of T
// int x = 1, t = 0;
// for(int i = 0; i < s.size(); i ++){
// int v = s[i] - 'a';
// if(!d[x].ch[v]){
// d[x].ch[v] = ++cnt;
// d[cnt] = node();
// }
// x = d[x].ch[v];
// if(t < p.size() && p[t] == i){
// d[x].val ++;
// t ++;
// }
// }
//}
//int query(string &s){
// int x = 1;
// for(int i = 0; i < s.size(); i ++){
// int v = s[i] - 'a';
// x = d[x].ch[v];
// }
// return d[x].val;
//}
a2 pbase[1000005];
void solve(){
pbase[0] = {1, 1};
for(int i = 1; i < 1000005; i ++)
pbase[i] = a2{pbase[i - 1][0] * base, pbase[i - 1][1] * base} % mod;
//不可水群!!!!
//戒焦躁!!!!
//专注!!!
int n;
while(cin >> n){
map<a2, int>cnt;
long long ans = 0;
vs a(n);
// cnt = 1;
// d[1] = node();
for(auto &s : a)
cin >> s;
sort(a.begin(), a.end(), [&](string &x, string &y)->bool{
return x.size() > y.size();
});
for(auto &s : a){
n = s.size();
vi p;
vector<a2>h(n + 1);
// set<int>spe;
for(int i = 0; i < n; i ++){
h[i + 1][0] = h[i][0] * base % mod[0] + s[i];
h[i + 1][1] = h[i][1] * base % mod[1] + s[i];
h[i + 1] = h[i + 1] % mod;
// spe.insert(s[i]);
}
auto getH = [&](int l, int r)->a2{
a2 res = a2{h[r][0] - h[l - 1][0] * pbase[r - l + 1][0], h[r][1] - h[l - 1][1] * pbase[r - l + 1][1]} % mod;
return a2{res[0] + mod[0], res[1] + mod[1]} % mod;
};
a2 H = getH(1, n);
H[0] = H[0] * qpow(pbase[n][0] - 1ll, mod[0] - 2, mod[0]) % mod[0];
H[1] = H[1] * qpow(pbase[n][1] - 1ll, mod[1] - 2, mod[1]) % mod[1];
ans += cnt[H];
cnt[H] ++;
// if(spe.size() == 1){
// p.assign(n, 0);
// iota(p.begin(), p.end(), 0);
// }
// else{
// for(int len = 2; len <= n; len ++){
// if(n % len)
// continue;
// bool f = 1;
// a2 ori = getH(1, len);
// for(int i = 2; i <= n / len; i ++){
// if(ori != getH(len * (i - 1) + 1, len * i)){
// f = 0;
// break;
// }
// }
// if(f)
// p.push_back(len - 1);
// }
// }
// ans += query(s);
// insert(s, p);
}
cout << ans << "\n";
}
return;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int ____=1;
// cin>>____;
while(____--)solve();
return 0;
}
//火车头长度 141
詳細信息
Test #1:
score: 100
Accepted
time: 397ms
memory: 23580kb
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