hdu6761lyndon分解
题:http://acm.hdu.edu.cn/showproblem.php?pid=6761
分析:每个位置的答案就是加入当前的字符后,该字符串lyndon分解后最后一个lyndon的最左边的位置
lyndon分解参考:https://blog.csdn.net/wayne_lee_lwc/article/details/107528945
#include<bits/stdc++.h> using namespace std; #define pb push_back #define MP make_pair typedef long long ll; const int mod=1e9+7; const int M=2e6+6; const int inf=0x3f3f3f3f; const ll INF=1e18; ll ans[M]; char s[M]; int main(){ int T; scanf("%d",&T); while(T--){ scanf("%s",s); int len=strlen(s); ans[0]=1; int nowi=0; while(nowi<len){ int k=nowi,j=nowi+1; while(j<len&&s[j]>=s[k]){ if(s[j]>s[k]){ ans[j]=nowi+1; k=nowi; } else{ ans[j]=ans[k]+j-k; k++; } j++; } while(nowi<=k) nowi+=j-k; if(nowi==j&&nowi<len) ans[j]=nowi+1; } ll res=0ll; for(int i=len-1;i>=0;i--) res=(res*1112ll%mod+ans[i])%mod; printf("%lld\n",res); } return 0; }