【CTSC2014】企鹅QQ
题面
https://www.luogu.org/problem/P4503
题解
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #define uLL unsigned long long #define ri register int #define N 30050 #define L 205 #define p1 107 #define p2 233 using namespace std; uLL A1[N][L],A2[N][L],B1[N][L],B2[N][L]; int id[N]; char s[N][L]; int n,l,ss; int sani; bool cmp(int x,int y){ return (A1[x][sani-1]<A1[y][sani-1] || A1[x][sani-1]==A1[y][sani-1] && A2[x][sani-1]<A2[y][sani-1] || A1[x][sani-1]==A1[y][sani-1] && A2[x][sani-1]==A2[y][sani-1] && B1[x][sani+1]<B1[y][sani+1]); } int main(){ scanf("%d %d %d",&n,&l,&ss); for (ri i=1;i<=n;i++) scanf("%s",s[i]+1); for (ri i=1;i<=n;i++) { for (ri j=1;j<=l;j++) { A1[i][j]=A1[i][j-1]*p1+s[i][j]; A2[i][j]=A2[i][j-1]*p2+s[i][j]; } for (ri j=l;j>=1;j--) { B1[i][j]=B1[i][j+1]*p1+s[i][j]; } } for (ri i=1;i<=n;i++) id[i]=i; long long ans=0LL; for (sani=1;sani<=l;sani++) { sort(id+1,id+n+1,cmp); int s=0; for (ri i=2;i<=n;i++) { if (A1[id[i]][sani-1]==A1[id[i-1]][sani-1] && A2[id[i]][sani-1]==A2[id[i-1]][sani-1] && B1[id[i]][sani+1]==B1[id[i-1]][sani+1]) ans+=++s; else s=0; } } cout<<ans<<endl; }