A:字符串hash+kmp
#include <iostream> #include <cstdio> #include <string> #include <map> using namespace std; typedef unsigned long long ull; typedef long long ll; const int maxn = 1e6+5; const int mod = 998244353; string s[maxn]; map<ull,int> hush; ull base = 233; void store(int x) { int len = s[x].length()-1; ull now = 0 , lev = 1; for(int i=len;i>=1;--i,lev=lev*base) { now = now + lev * (s[x][i]-'a'+1); ++hush[now]; } } int ne[maxn]; ll cnt[maxn]; ll pro(int x) { int len = s[x].length()-1; ne[1] = 0; for(int i=2,j=0;i<=len;++i) { while(j>=1&&s[x][j+1]!=s[x][i]) j=ne[j]; if(s[x][j+1]==s[x][i]) ++j; ne[i]=j; } ull now = 0; for(int i=1;i<=len;++i) { now = now * base + s[x][i]-'a'+1; cnt[i] = hush[now]; cnt[ne[i]] -= cnt[i]; } ll res = 0; for(int i=1;i<=len;++i) { res = (res + (ll)i*i%mod*cnt[i]%mod)%mod; } return res; } int main() { // freopen("in.txt","r",stdin); ios::sync_with_stdio(false); int n; cin>>n;; for(int i=1;i<=n;++i) cin>>s[i] , s[i]="0"+s[i] ,store(i); ll ans = 0; for(int i=1;i<=n;++i) ans = (ans + pro(i))%mod; cout<<ans; return 0; }