2020杭电多校第一场 1011-Minimum Index lyndon分解+kmp

1011-Minimum Index

题意

给你一个字符串\(s\),问字符串\(s\)的每个前缀的字典序最小的后缀是哪个,设每个前缀的最小后缀的下标分别为\(t_1,t_2,\dots,t_n\),计算\(\sum_{i=1}^{n}t_i\times 1112^{i-1}\)

分析

先做一遍lyndon分解,lyndon串的字典序严格小于它的所有后缀的字典序,且分解出的串\(w_1,w_2,\dots,w_n\)的字典序是非严格单调递减的,对分解出的每个lyndon串分别处理:

设这个lyndon串为\(t\),对于它的一个前缀\(t_i\),如果存在一个最小的\(j\),使得\(t[1\sim j]=t[i-j+1\sim i]\),即\(t_i\)的最短公共前后缀,那么\(i-j+1\)就是前缀\(t_i\)的最小后缀下标,可以用\(kmp\)跑出\(next\)数组后\(dp\)计算一下,\(dp[i]=min(dp[i],dp[nex[i]])\)

Code

#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<sstream>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define per(i,n,x) for(int i=n;i>=x;i--)
#define sz(a) int(a.size())
#define rson mid+1,r,p<<1|1
#define pii pair<int,int>
#define lson l,mid,p<<1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps=1e-8;
const int mod=1e9+7;
const int N=1e6+10;
const int inf=1e9;
vector<string>v;
void duval(string const& s) {
  int n = s.size(), i = 0;
  while (i < n) {
    int j = i + 1, k = i;
    while (j < n && s[k] <= s[j]) {
      if (s[k] < s[j])
        k = i;
      else
        k++;
      j++;
    }
    while (i <= k) {
      v.push_back(s.substr(i, j - k));
      i += j - k;
    }
  }
}
int T;
string s;
int nex[N],dp[N];
void get(string const &s){
	int n=s.size();
	nex[0]=-1;
	rep(i,0,n-1) dp[i]=i;
	for(int i=1,j=-1;i<n;i++){
		while(~j&&s[j+1]!=s[i]) j=nex[j];
		if(s[j+1]==s[i]) ++j;
		nex[i]=j;
		if(~j) dp[i]=min(dp[i],dp[j]);
	}
}
int main(){
	ios::sync_with_stdio(false);
	//freopen("in","r",stdin);
	cin>>T;
	while(T--){
		v.clear();
		cin>>s;
		duval(s);
		int l=1;
		ll ret=1;
		ll ans=0;
		for(string x:v){
			get(x);
			rep(i,0,sz(x)-1){
				ans=(ans+1ll*(l-dp[i])*ret%mod)%mod;
				++l;
				ret=ret*1112%mod;
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}
posted @ 2020-07-26 14:41  xyq0220  阅读(155)  评论(0编辑  收藏  举报