kmjp's blog

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

Codeforces #291 Div2 C. Watto and Mechanism

SegFaultするはずのコードが通ってしまい困惑。
おかげで順位は良かったんだけどね…。
http://codeforces.com/contest/514/problem/C

問題

'a''b''c'だけで構成されるN個の文字列S[i]がある。

M個のクエリ文字列T[i]が与えられる。
T[i]の1文字だけを別の文字に入れ替えたとき、Sの何れかと一致するかを答えよ。

解法

オーソドックスな解法はローリングハッシュ。
T[i]のハッシュ値から、うまく1文字だけ差し替えたときのハッシュ値を計算してS[i]のハッシュ値と比較すればよい。

自分の本番解は、T[i]のサイズによる分割。
T[i]のサイズが300文字以下の場合、1文字だけ差し替えたものを作成して総当たり。
T[i]のサイズが300文字超の場合、同じ文字列長のS[i]と総当たり。
長い文字列の数はそんなにないので、これでも間に合う。

以下両方のコードを記載。

struct RollingHash {
	static const ll mo0=1000000007,mo1=1000000009;
	static const ll mul0=10009,mul1=10007;
	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(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) 
		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];

int N,M;
string S[400000];
string T;
set<pair<ll,ll> > SS[340000];

void solve() {
	int i,j,k,l,r,x,y; string s;
	cin>>N>>M;
	
	FOR(i,N) {
		RollingHash rh;
		cin>>S[i];
		if(S[i].size()>300000) continue;
		SS[S[i].size()].insert(rh.hash(S[i]));
	}
	
	RollingHash abc[3];
	abc[0].init("a");
	abc[1].init("b");
	abc[2].init("c");
	
	while(M--) {
		cin>>T;
		bool ok=false;
		
		if(T.size()<=300000) {
			RollingHash rh;
			rh.init(T);
			FOR(i,T.size()) FOR(x,3) if(T[i]-'a' != x) {
				pair<ll,ll> h;
				h=RollingHash::concat(rh.hash(0,i-1),abc[x].hash(0,0),1);
				h=RollingHash::concat(h,rh.hash(i+1,T.size()-1),T.size()-1-i);
				ok |= SS[T.size()].count(h);
			}
		}
		
		cout << (ok?"YES":"NO") << endl;
	}
	
}
int N,M;
string S[400000];
string T;
set<string> SS[340000];
vector<string> VV[340000];

void solve() {
	int i,j,k,l,r,x,y; string s;
	cin>>N>>M;
	
	FOR(i,N) {
		cin>>S[i];
		if(S[i].size()>300000) continue;
		SS[S[i].size()].insert(S[i]);
		VV[S[i].size()].push_back(S[i]);
	}
	while(M--) {
		cin>>T;
		bool ok=false;
		
		if(T.size()<300) {
			FOR(i,T.size()) {
				char org=T[i];
				FOR(x,3) {
					if(x+'a'==org) continue;
					T[i]=x+'a';
					ok |= SS[T.size()].count(T);
				}
				T[i]=org;
			}
			
		}
		else if(T.size()<=300000) {
			FOR(i,VV[T.size()].size()) {
				string& V=VV[T.size()][i];
				int dif=0;
				FOR(x,V.size()) {
					dif += V[x]!=T[x];
					if(dif>1) break;
				}
				ok |= dif==1;
			}
		}
		
		cout << (ok?"YES":"NO") << endl;
	}
	
}

まとめ

ローリングハッシュライブラリを拡充した。