kmjp's blog

競技プログラミング参加記です

Codeforces #662 Div2 E2. Twilight and Ancient Scroll

これまたECRみたいな問題。
https://codeforces.com/contest/1393/problem/E2

問題

N個の文字列が与えられる。
各文字列から1文字任意の文字を除いた時、文字列順が辞書順昇順になるようにしたい。
文字列の除き方は何通りか。

解法

各文字列について、取り除き方を列挙し、かつそれを昇順に並べておく。
そうすれば、文字列を順にみたとき、尺取り法の要領で、辞書順昇順を保つような直前の文字列の組み合わせの総和を計算できる。
この時の文字列比較は、RollingHashを使い高速に行えるようにしておこう。

問題は、文字列から1文字除いた時の文字列を昇順に並べる方法である。
これはうまくdequeを使うとO(文字列長)で行える。

using VT = string;

struct RollingHash {
	static const ll mo0=1000000021,mo1=1000000009;
	static ll mul0,mul1;
	static const ll add0=1000010007, add1=1003333331;
	static vector<ll> pmo[2];
	VT s; int l; vector<ll> hash_[2];
	void init(VT s) {
		this->s=s; l=s.size(); int i,j;
		hash_[0]=hash_[1]=vector<ll>(1,0);
		if(!mul0) mul0=10009+(((ll)&mul0+time(NULL))>>5)%259,mul1=10007+(((ll)&mul1)>>5)%257;
		if(pmo[0].empty()) pmo[0].push_back(1),pmo[1].push_back(1);
		FOR(i,l) hash_[0].push_back((hash_[0].back()*mul0+add0+s[i])%mo0);
		//FOR(i,l) hash_[1].push_back((hash_[1].back()*mul1+add1+s[i])%mo1);
	}
	pair<ll,ll> hash(int l,int r) { // s[l..r]
		if(l>r) return make_pair(0,0);
		while(pmo[0].size()<r+2)
			pmo[0].push_back(pmo[0].back()*mul0%mo0);// pmo[1].push_back(pmo[1].back()*mul1%mo1);
		return make_pair((hash_[0][r+1]+(mo0-hash_[0][l]*pmo[0][r+1-l]%mo0))%mo0,
			             0);
		return make_pair((hash_[0][r+1]+(mo0-hash_[0][l]*pmo[0][r+1-l]%mo0))%mo0,
			             (hash_[1][r+1]+(mo1-hash_[1][l]*pmo[1][r+1-l]%mo1))%mo1);
	}
	static pair<ll,ll> concat(pair<ll,ll> L,pair<ll,ll> R,int RL) { // hash(L+R) RL=len-of-R
		while(pmo[0].size()<RL+2) pmo[0].push_back(pmo[0].back()*mul0%mo0), pmo[1].push_back(0);
		return make_pair((R.first + L.first*pmo[0][RL])%mo0,0);
		/*
		while(pmo[0].size()<RL+2) pmo[0].push_back(pmo[0].back()*mul0%mo0), pmo[1].push_back(pmo[1].back()*mul1%mo1);
		return make_pair((R.first + L.first*pmo[0][RL])%mo0,(R.second + L.second*pmo[1][RL])%mo1);
		*/
	}
};
vector<ll> RollingHash::pmo[2]; ll RollingHash::mul0,RollingHash::mul1;


int N;
string S[101010];
deque<int> Q[101010];
vector<ll> dp[101010];
RollingHash rh[101010];

const ll mo=1000000007;

pair<ll,ll> gethash(int i,int p,int len) {
	if(len<=p) {
		return rh[i].hash(0,len-1);
	}
	pair<ll,ll> L=rh[i].hash(0,p-1);
	pair<ll,ll> R=rh[i].hash(p+1,len);
	return rh[i].concat(L,R,len-p);
}

int comp(int i,int p,int j,int q) {
	int a=(int)S[i].size()-(p<S[i].size());
	int b=(int)S[j].size()-(q<S[j].size());
	int ml=min(a,b);
	int cur=0;
	int x;
	for(x=20;x>=0;x--) if(cur+(1<<x)<=ml) {
		auto L=gethash(i,p,cur+(1<<x));
		auto R=gethash(j,q,cur+(1<<x));
		if(L==R) cur+=1<<x;
	}
	
	if(cur==ml) {
		return a<=b;
	}
	else {
		char c=S[i][cur+(cur>=p)];
		char d=S[j][cur+(cur>=q)];
		return c<=d;
	}
	
	
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	Q[0].push_back(0);
	dp[0].push_back(1);
	rh[0].init(S[0]);
	
	FOR(i,N) {
		cin>>S[i+1];
		rh[i+1].init(S[i+1]);
		dp[i+1].resize(S[i+1].size()+1);
		
		vector<vector<int>> cand;
		j=0;
		while(j<S[i+1].size()) {
			cand.push_back({0,j,1});
			x=j+1;
			while(x<S[i+1].size()&&S[i+1][j]==S[i+1][x]) x++, cand.back()[2]++;
			if(x==S[i+1].size()||S[i+1][j]>S[i+1][x]) cand.back()[0]=1;
			j=x;
		}
		
		
		reverse(ALL(cand));
		Q[i+1].push_back(S[i+1].size());
		FORR(c,cand) {
			if(c[0]) {
				FOR(j,c[2]) Q[i+1].push_front(c[1]+j);
			}
			else {
				FOR(j,c[2]) Q[i+1].push_back(c[1]+j);
			}
		}
		int p=0,q;
		ll sum=0;
		FOR(q,Q[i+1].size()) {
			while(p<Q[i].size() && comp(i,Q[i][p],i+1,Q[i+1][q])) (sum+=dp[i][p++])%=mo;
			//cout<<q<<" "<<S[i+1].substr(0,Q[i+1][q])<<S[i+1].substr(Q[i+1][q]+1)<<" "<<p<<" "<<sum<<endl;
			dp[i+1][q]=sum;
		}
	}
	ll sum=0;
	FORR(d,dp[N]) sum+=d;
	cout<<sum%mo<<endl;
	
}

まとめ

実装が面倒な問題。