C. Watto and Mechanism(哈希 | 字典树 + dfs (树上dfs))

题目链接

题意 

给出n个模式串,给出m个字符串,问字符串能否通过变换一次成为模式串(必须变换)

收获

  • 手动取模必须用long long, long long hash = 0,如果不开long long中间hash*h会变成负数,不能用负数取模,严禁!所以要开long long,mod = 1e9 + 7不代表可以用int,相反应该用long long 这样能保证不会是负数
  • 这个题没法用ull自动溢出,mod和h取值也卡一般的值,且卡单哈希

seed=17 ,MOD=1e9+9
seed=257,MOD=1e9+7
seed=1009 ,MOD=1e15+37
seed=99959

#include<bits/stdc++.h>
#define PII pair<int,int>
#define PLL pair<long long,long long>
#define fi first
#define se second
#define endl '\n'
#define bug printf("bug");
using namespace std;
const int N=6e5+10;
const int INF=0x3f3f3f3f;
const long long LNF=0x3f3f3f3f3f3f3f3f;
const long long hash_mod=1e12+39,h=31;
long long pow1[N],hash_res[N];
char s[N];
int n,m;
 
bool find(){
	int len=strlen(s+1);
	long long hash=0;
	for(int i=1;i<=len;i++){
		hash=(hash*h+s[i]-'a')%hash_mod;
	}
	for(int i=1;i<=len;i++){
		for(int j='a';j<='c';j++){
			if(j==s[i]) continue;
			long long x=(hash+(j-s[i])*pow1[len-i]%hash_mod+hash_mod)%hash_mod;
			if(*lower_bound(hash_res+1,hash_res+n+1,x)==x) return true;
		}
	}
	return false;
}
 
void solve(){
	scanf("%d %d",&n,&m);
	pow1[0]=1;
	for(int i=1;i<=N-10;i++) pow1[i]=pow1[i-1]*h%hash_mod;
	for(int i=1;i<=n;i++){
		scanf("%s",s+1);
		int len=strlen(s+1);
		long long hash=0;
		for(int j=1;j<=len;j++){
			hash=(hash*h+s[j]-'a')%hash_mod;
		}
		hash_res[i]=hash;
	}
	sort(hash_res+1,hash_res+n+1);
	for(int i=1;i<=m;i++){
		scanf("%s",s+1);
		if(find()) printf("YES\n");
		else printf("NO\n");
	}
}
 
int main(){
	int t; t=1;
	//scanf("%d",&t);
	while(t--){
		solve();
	}
	return 0 - 0;
}

同一思路,用set判断

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn = 1e6 + 5;
const int has = 1009;//257,17,1009
const int MOD = 1e15+37;//1e9+7,1e9+9,1e15+37
ll p[maxn];
int n, m;
set<ll>S;
void Hash(char *s) {
    ll h = 0;
    int len = strlen(s);
    for(int i = 0; i < len; i++) h = (h * has + s[i]+MOD)%MOD ;
    S.insert(h);
}
void POW() {
    p[0] = 1;
    for(int i = 1; i <= maxn; i++) {
        p[i] = (p[i - 1] * has)%MOD ;
    }
}
bool ok(char *s) {
    int len = strlen(s);
    ll h = 0;
    for(int i = 0; i < len; i++) {
        h = (h * has + s[i]+MOD)%MOD ;
    }
    for(int i = 0; i < len; i++) {
        for(char c = 'a'; c <= 'c'; c++) {
            if(c == s[i]) continue;
//            if(S.find((h + (c - s[i])*p[len - i - 1] ) ) != S.end()) return 1;
            if(S.find((((c - s[i]) * p[len - i - 1] + h) % MOD + MOD) % MOD) != S.end()) return 1;

        }
    }
    return 0;
}
char a[maxn];
int main() {
    POW();
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) {
        scanf("%s", a);
        Hash(a);
    }
    for(int i = 1; i <= m; i++) {
        scanf("%s", a);
        if(ok(a)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

双哈希

const int maxn=600100;
const int maxm=100100;
const int up=100000;
const int hashp=13331;
const int hashpp=131;
//给定n个串,然后给出m个询问。
//对于每个询问,判断 能否 更改一个字符使得其变为n个串中的一个
//串只有abc三种字符。
//hash乱搞+枚举

llu p[maxn],hs[maxn];
llu pp[maxn],hhs[maxn];
char s1[maxn],s2[maxn];
int n,m;
set<llu>se,see;

llu get(void)
{
    int len=strlen(s1);
    llu ans=0;
    for(int i=0;i<len;i++)
        ans=ans*hashp+s1[i];
    return ans;
}

llu get2(void)
{
    int len=strlen(s1);
    llu ans=0;
    for(int i=0;i<len;i++)
        ans=(ans*hashpp+s1[i])%mod;
    return ans;
}

void init(void)
{
    p[0]=1;
    for(int i=1;i<maxn;i++)
        p[i]=p[i-1]*hashp;
    pp[0]=1;
    for(int i=1;i<maxn;i++)
        pp[i]=pp[i-1]*hashpp%mod;
}

bool check(void)
{
    int len=strlen(s2+1);
    for(int i=1;i<=len;i++)
        hs[i]=hs[i-1]*hashp+s2[i];
    llu pre,last;
    for(int i=1;i<=len;i++)
    {
        pre=hs[i-1]*p[len-i+1];
        last=hs[len]-hs[i]*p[len-i];
        if(s2[i]!='a'&&se.find(pre+'a'*p[len-i]+last)!=se.end()) return true;
        if(s2[i]!='b'&&se.find(pre+'b'*p[len-i]+last)!=se.end()) return true;
        if(s2[i]!='c'&&se.find(pre+'c'*p[len-i]+last)!=se.end()) return true;
    }
    return false;
}

bool check2(void)
{
    int len=strlen(s2+1);
    for(int i=1;i<=len;i++)
        hhs[i]=(hhs[i-1]*hashpp+s2[i])%mod;
    llu pre,last;
    for(int i=1;i<=len;i++)
    {
        pre=(hhs[i-1]*pp[len-i+1])%mod;
        last=(hhs[len]-hhs[i]*pp[len-i]%mod+mod)%mod;
        if(s2[i]!='a'&&see.find((pre+'a'*pp[len-i]+last)%mod)!=see.end()) return true;
        if(s2[i]!='b'&&see.find((pre+'b'*pp[len-i]+last)%mod)!=see.end()) return true;
        if(s2[i]!='c'&&see.find((pre+'c'*pp[len-i]+last)%mod)!=see.end()) return true;
    }
    return false;
}

int main(void)
{
    init();
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s1);
        se.insert(get());
        see.insert(get2());
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%s",s2+1);
        if(check()&&check2()) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


字典树 + dfs

树上dfs必备:

        p:当前树的节点          d:树的深度,也是当前字符串到第几个字母

PS:注意树的dfs,深度是不是一个定值,如果是的话,那么dfs的时间复杂度完全够

不要遇到dfs就感觉时间要炸

flag根据题目条件来写的

bool类型的dfs,先根据传入的参数来确定,什么时候一定是true(除了这种情况其他都是false了),什么时候一定是false(比如d > len一定不行)。

写完这些,再写什么时候往下走,在把往下走的回溯的结果作为新的if条件返回true 

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 6 * 1e5 + 10;
int son[N][3];
bool vis[N];
string s;
int len, n, m, cnt;

bool work(int p, int d, int flag)
{
    if(d == len && flag && vis[p]) return true;
    if(d > len) return false;
    int k = s[d] - 'a';
    if(son[p][k]) {
        if(work(son[p][k], d + 1, flag)) return true;
    }
    if(flag)return false;
    for (int i = 0; i < 3; i ++ ) {
        if(k == i)continue;
        if(son[p][i]) {
            if(work(son[p][i], d + 1, flag ^ 1))return true;
        }
    }
    return false;
}

int main(){
    //std::ios::sync_with_stdio(false);
    //std::cin.tie(nullptr);
    cin >> n >> m;
    for (int i = 0; i < n; i ++ ) {
        cin >> s;
        int p = 0;
        for (int i = 0; s[i]; i ++ ) {
            int v = s[i] - 'a';
            if(!son[p][v]) son[p][v] = ++ cnt;
            p = son[p][v];
        }
        vis[p] = 1;
    }
    for (int i = 0; i < m; i ++ ) {
        cin >> s;
        len = (int)s.size();
        if(work(0, 0, 0)) puts("YES");
        else puts("NO");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值