不太理解 RE 的原因
查看原帖
不太理解 RE 的原因
694172
Dawn_chen楼主2024/1/1 21:57

RT, 感觉下面代码中没有什么可以 RE 的地方, 还是莫名 RE on #3。

记录。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 1000010
#define int long long

const int mod = 100007; 
string s[N];
int n, m, val[N], vis[N];

int hsh(string x) {
	int res = 0, bse = 10086;
	for(int i = 0; i <= x.size() - 1; i ++) {
		res = res % mod + ((x[i] - 'a') * bse % mod) % mod;
		bse = bse * 10086;
	}
	return abs(abs(res) % mod);
}

int dfs(string x) {
	int cnt = 0, res = 0;
	for(int i = 0; i <= x.size() - 1; i ++) {
		if(x[i] == '?') {
			cnt ++;
			string y = x;
			y.erase(i, 1);
			if( ! vis[hsh(y)]) {
				vis[hsh(y)] = 1;
				res += dfs(y);
			}
			for(int now = 0; now < 5; now ++) {
				x[i] = (char)('a' + now);
				if( ! vis[hsh(x)]) {
					vis[hsh(x)] = 1;
					res += dfs(x);
				}
			}
		}
	}
	if( ! cnt) return val[hsh(x)];
	else return res;
}

signed main() {
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for(int i = 1; i <= n; i ++) {
		cin >> s[i];
		val[hsh(s[i])] ++;
	}
	while(m --) {
		string a; cin >> a;
		cout << dfs(a) << "\n";
		memset(vis, 0, sizeof vis);
	}
	return 0;
}
2024/1/1 21:57
加载中...