NC221779. BirthdayCake
描述
输入描述
The first line contains one integer -- the number of cakes.
The next lines describe all the cakes, where the -th line contains one string consisting of lowercase Latin letters.
It is guaranteed that the sum of lengths of cakes does not exceed .
输出描述
Print one integer -- the number of pairs of cakes meet the above condition.
示例1
输入:
3 ab ab cabc
输出:
3
示例2
输入:
3 abc a cabc
输出:
0
示例3
输入:
4 hhh hhh hhh hhh
输出:
6
C++ 解法, 执行用时: 109ms, 内存消耗: 33992K, 提交时间: 2021-05-28 00:06:02
#pragma GCC optimize("-Ofast","-funroll-all-loops") #include<bits/stdc++.h> #define int long long #define LL __int128 using namespace std; const int N=4e5+10; const int base=131,mod=1e18+3; LL h[N],pw[N]; int n,id[N],res; string str[N]; unordered_map<int,int> mp; LL get(int l,int r){return (h[r]-h[l-1]*pw[r-l+1]%mod+mod)%mod;} signed main(){ cin>>n; pw[0]=1; for(int i=1;i<=n;i++) cin>>str[i],id[i]=i; for(int i=1;i<N;i++) pw[i]=pw[i-1]*base%mod; sort(id+1,id+1+n,[](int a,int b){return str[a].size()<str[b].size();}); for(int i=1,m;i<=n;i++){ h[0]=str[id[i]][0],m=str[id[i]].size()-1; for(int j=1;j<=m;j++) h[j]=(h[j-1]*base+str[id[i]][j])%mod; for(int j=(m+1)/2;j<m;j++) if(get(0,m-j-1)==get(j+1,m)) res+=mp[get(m-j,j)]; res+=mp[h[m]]++; } cout<<res; return 0; }