HDU 4886 TIANKENG’s restaurant(Ⅱ) ( 暴力+hash )

TIANKENG’s restaurant(Ⅱ)

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 130107/65536 K (Java/Others)
Total Submission(s): 466    Accepted Submission(s): 153

Problem Description

After improving the marketing strategy, TIANKENG has made a fortune and he is going to step into the status of TuHao. Nevertheless, TIANKENG wants his restaurant to go international, so he decides to name his restaurant in English. For the lack of English skills, TIANKENG turns to CC, an English expert, to help him think of a property name. CC is a algorithm lover other than English, so he gives a long string S to TIANKENG. The string S only contains eight kinds of letters-------‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’. TIANKENG wants his restaurant’s name to be out of ordinary, so the restaurant’s name is a string T which should satisfy the following conditions: The string T should be as short as possible, if there are more than one strings which have the same shortest length, you should choose the string which has the minimum lexicographic order. Could you help TIANKENG get the name as soon as possible?

Meanwhile, T is different from all the substrings of S. Could you help TIANKENG get the name as soon as possible?

Input

The first line input file contains an integer T(T<=50) indicating the number of case.
In each test case:
Input a string S. the length of S is not large than 1000000.

Output

For each test case:
Output the string t satisfying the condition.(T also only contains eight kinds of letters-------‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’.)

Sample Input

3

ABCDEFGH

AAABAACADAEAFAGAH

ACAC

Sample Output

AA

BB

B

因为只有8个字母 。

那么先枚举长度 ( i = 1 ~ 8 )。

O(n)处理出该长度下主串存在的子串。

再枚举排列 ( j = 0 ~ i ! )判存。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>

using namespace std;

typedef long long LL;
typedef pair<int,int> pii;
const int N = 1000010;
const int M = 43046721;
const int inf = 1e9+7;
const double eps = 1e-8;
bool vis[M];
char str[N];
int a[N],f[N],len;

void get_s( int n , int cnt ,char *s) {
    for( int i = cnt-1 ; i >= 0 ; --i ){
        s[i] = (n%8)+‘A‘;
        n/=8;
    }
    s[cnt] = 0 ;
}

int check( int n ) {
    int tmp = 0 ;
    if( len < n ) return -1;
    for( int i = 0 ; i < f[n] ; ++i ) vis[i] = false ;
    for( int i = 0 ; i < n ; ++i )
        tmp = tmp * 8 + a[i] ;
    vis[tmp] = true ;
    for( int i = n ; i < len ; ++i ){
        tmp = ( tmp % f[n-1] ) * 8 + a[i] ;
        vis[tmp] = true ;
    }
    for( int i = 0 ; i < f[n] ; ++i )if(!vis[i]){
        return i ;
    }
    return -1 ;
}

void Run(){
    scanf("%s",str); len = strlen(str);
    for( int i = 0 ; i < len ; ++i ) a[i] = str[i]-‘A‘;
    for( int i = 1 ; i <= 8 ; ++i ){
        int res = check(i) ;
        if( res == -1 ) continue ;
        get_s(res,i,str) ;
        puts(str);
        return ;
    }
}

int main(){
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif // LOCAL
    int tot = 1 , m = 1 ; f[0] = 1 ;
    for( int i = 0 ; i < 8 ; ++i ) m *=8 ,f[tot++] = m ;
    int _ ; scanf("%d",&_);
    while( _-- ) Run();
}

时间: 2024-07-28 23:07:31

HDU 4886 TIANKENG’s restaurant(Ⅱ) ( 暴力+hash )的相关文章

hdu 4886 TIANKENG’s restaurant(2)(hash+暴力)

题目链接:hdu 4886 TIANKENG's restaurant(2) 题目大意:给定一个字符串S,要求在该字符串中找到一个最短并且字符串字典序最小. 解题思路:每次枚举字符串的长度,然后将S中所有该长度的子串映射成一个9进制数,最后再遍历一遍标记数组. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1000005; i

hdu 4886 TIANKENG’s restaurant(Ⅱ) (hash)

题目大意: 求出在文本串中第一个没出现的字典序最小的串.. 思路分析: 开始的时候 用后缀数组写,然后根据sa的有序性.你就可以知道哪个串没有出现了. 但是题目卡了倍增哦... 自习想一想的话,我们用 sa 数组,也就是想知道这个串有没有出现过,也就是判断重复,却浪费了  O (n * lg n)... 判断重复为什么没想到hash . 把每一个长度的子串都hash 出来,用八进制表示,这样的话就可以得到一个递增的hash值. 将之存入hash 数组.找到第一个空的hash的下标,就是第一个没出

HDU 4886 TIANKENG’s restaurant(Ⅱ)

题意: 一个字符串有许多子串  现要找出最短的字典序最小的不是它的子串的串  这个长串只有A~H字母 思路: YY一下答案串能有多长  8^7就比长串长了  所以也就是7的长度 那么只需要枚举长度  利用哈希判定字符串出现的问题  如何哈希呢? 一共就8个字母明显搞成8进制数  例如  AABCAD 就是 001203(8)  只有7的长度连int都不会爆  哈希稳稳的 而且通过hash值可以很简单的转化回字符串  输出也方便 代码: #include<cstdio> #include<

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> #

hdu 4883 TIANKENG’s restaurant(暴力)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come t

HDU 4883 TIANKENG’s restaurant Bestcoder 2-1(模拟)

TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of t

HDU 4883 TIANKENG’s restaurant(区间选点)

HDU 4883 TIANKENG's restaurant 题目链接 题意:给定一些时间作为区间,和一个人数,问要安排多少人去看管(一个人只能看管一个人) 思路:普通的区间选点问题,一个区间拆成一个进入点一个出去点,然后排序循环求答案即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 20005; struct Man {

HDU 4883 TIANKENG’s restaurant (贪心)

链接:带我学习,带我飞 第一次BC,稳挂,WA n多次,今天重新做了一下 略挫 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <string> #include <vector> #include <set> #include <algorithm>

HDU 4883 TIANKENG’s restaurant (区间更新)

Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come to enjoy their meal, and there are Xi persons in the