kmjp's blog

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

AtCoder ARC #060 : F - 最良表現 / Best Representation

解けるとは思わなかった。
http://arc060.contest.atcoder.jp/tasks/arc060_d

問題

良い文字列でない文字列とは、別の同じ文字列を2回以上繰り返して連結して作れるものを示す。
文字列Wを幾つかの良い文字列に分割したい。
最少何個の文字列に分割すればよいか。またそのような分割は何通りあるか。

解法

(恐らく)良くない文字列は、その文字列が2種類以上の文字で構成されている場合、1文字増減すればいずれも良い文字列になる。
よって、Wが同じ文字だけで構成されている場合は全部1文字ずつに分解するしかないので|W|個の文字列に分解しよう。

それ以外の場合、最少で1個か2個の分解すればよい。
(良くない文字列2つに分割できた場合、片方を1文字増やし、片方を1文字減らせばどちらも良い文字列になるので、少なくとも1つは2つの文字列の分割方法が存在する)

WのL文字のprefix W[0...(L-1)]に対し、Lのk倍(kは2以上の整数)のprefixが先頭L文字のprefixのk倍と一致するなら、分割した前半はそのような文字列を取れない。
これはW[0...(L-1)]とW[(L*(d-1))...(L*d-1)]を順次一致判定していけば求められる。この判定はローリングハッシュで高速化可能。
同様にsuffixについても、ローリングハッシュで良い文字列かどうか判定していく。
(Z-Algorithhmでもよい)

あとはprefix W[0...x]とsuffix W[(x+1)...(L-1)]が共に良い文字列であるxの数を求めればよい。
また、W[0...(L-1)]が良い文字列なら、分割は不要となる。

struct RollingHash {
	static const ll mo0=1000000007,mo1=1000000009;
	static ll mul0,mul1;
	static const ll add0=1000010007, add1=1003333331;
	static vector<ll> pmo[2];
	string s; int l; vector<ll> hash_[2];
	void init(string s) {
		this->s=s; l=s.size(); int i,j;
		hash_[0]=hash_[1]=vector<ll>(1,0);
		if(!mul0) mul0=10009+(((ll)&mul0)>>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,
			             (hash_[1][r+1]+(mo1-hash_[1][l]*pmo[1][r+1-l]%mo1))%mo1);
	}
	pair<ll,ll> hash(string s) { init(s); return hash(0,s.size()-1); }
	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(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;

string W;
int L;
int prefixNG[505050];
int suffixNG[505050];
RollingHash rh;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>W;
	rh.init(W);
	L=W.size();
	
	// allsame;
	FOR(i,26) {
		if(count(W.begin(),W.end(),'a'+i)==L) return _P("%d\n%d\n",L,1);
	}
	
	for(l=1;l<=L;l++) if(prefixNG[l-1]==0) {
		for(x=l;x+l<=L;x+=l) {
			if(rh.hash(0,l-1)!=rh.hash(x,x+l-1)) break;
			prefixNG[x+l-1]=1;
		}
	}
	if(prefixNG[L-1]==0) return _P("1\n1\n");
	for(l=1;l<=L;l++) if(suffixNG[L-l]==0) {
		for(x=L-l-l;x-l>=0;x-=l) {
			if(rh.hash(x,x+l-1)!=rh.hash(x+l,x+l+l-1)) break;
			suffixNG[x]=1;
		}
	}
	int tot=0;
	FOR(i,L-1) if(prefixNG[i]==0 && suffixNG[i+1]==0) tot++;
	_P("%d\n%d\n",2,tot);
}

まとめ

意外と実行時間短かった。