https://www.lydsy.com/JudgeOnline/problem.php?id=3555
枚举每一位字符,计算字符两侧的哈希值,然后进行比较,用map或排序记录出与其相同的字符串数量。
时间复杂度O(nlogn)
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <cstdio> using namespace std; #define LL long long #define N int(3e4+2) #define M int(2e2+2) #define base1 233 #define base2 211 int n,m,l,ans; char s[M]; unsigned long long temp[N],before[N][M],behind[N][M]; void work(int x) { for(int i=1;i<=l;i++)before[x][i]=before[x][i-1]*149+s[i]; for(int i=l;i>=1;i--)behind[x][i]=behind[x][i+1]*137+s[i]; } int main() { scanf("%d%d%d",&n,&l,&m); for(int i=1;i<=n;i++) { scanf("%s",s+1); work(i); } for(int j=1;j<=l;j++) { for(int i=1;i<=n;i++)temp[i]=before[i][j-1]*233+behind[i][j+1]*213; sort(temp+1,temp+1+n); int now=1; for(int i=2;i<=n;i++) { if(temp[i]==temp[i-1])ans+=now,now++; else now=1; } } printf("%d",ans); }