首页 > 代码库 > HDU 4886 TIANKENG’s restaurant(Ⅱ) hash+dfs

HDU 4886 TIANKENG’s restaurant(Ⅱ) hash+dfs

题意:

1、找一个字符串s使得 s不是给定母串的子串

2、且s要最短

3、s在最短情况下字典序最小

hash,,,结果t掉了。。。加了个姿势怪异的hash值剪枝才过。。

#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
#define N 1000100
#define ll long long
#define mod 2496764 
char s[N];
short h[8][mod], tim;
bool f = false;
bool dfs(ll top, ll siz, ll dep) {
    if (siz > 1000000) return false;
    if(top == dep)
    {
        for(ll i = 0; i < 8; i++)
        {
            if(h[top][siz * 8 + i] != tim)
            {
                s[top] = i + 'A';
                s[top+1] = 0;
                f = true;
                return true;
            }
        }
        return false;
    }
    for(ll i = 0; i < 8; i++)
    {
        s[top] = i + 'A';
        if(dfs(top+1, siz * 8 + i, dep))return true;
    }
    return false;
}

int main(){
    int i, j, T; scanf("%d",&T);
    tim = 0;

    while(T--) {
        tim ++;
        scanf("%s", s);
        f = false;
        for(i = 0; s[i]; i++)
        {
            ll ans = 0;
            for(j = 0; j < 7 && s[i+j]; j++)
            {
                ans = ans * 8 + s[i + j] - 'A';
                h[j][ans] = tim;
            }
        }

       for(i = 0; i < 8; i++)
            if(dfs(0, 0, i))break;
        puts(s);
    }
    return 0;
}