1. 程式人生 > >hdu 4886 字串 hash+暴力

hdu 4886 字串 hash+暴力

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

題意:
給定一個由A~H8種字元組成,長度不超過1000000的字串。問在這個字串的最短不存在連續子串。若存在多個最短連續子串,輸出字典序最小的一個。
題解:
先估計最長的答案是多少?我的估計是6個字元長,為什麼?長度為6的字串共有8^6≈250000,如果不存在公用的情況,需要長度250000*6的長度,已經長過1000000了。(有可能是7個字元長的,但7個字元的樣例必須考慮字元共用,很難弄出來,所以6個字元就夠了,如果想要保證正確性,可以用7個字元的)。
然後找出所有長度小於等於6的所有子串,由於子串是連續的,所以只用6*n的時間去查詢。用f[i][j]標記存在的子串,i表示長度,j表示字元的雜湊值(字元種類只有8個,用i位8進位制就可以表示所有的情況)。最後遍歷f陣列,找出第一個未被標記的子串即可。

#include <bits/stdc++.h>
using namespace std;
int vis[9000005];
char s[1000005];
char zimu[9]={'A','B','C','D','E','F','G','H'};
int len;
char ans[1000005];
int solve(int x)
{
    memset(vis,0,sizeof(vis));
    for(int i=0;i<=len-x;i++)
    {
        int k=0;
        int sum=0;
        while(k<x)
        {
            sum=sum*8+s[i+k]-'A';
            k++;
        }
        vis[sum]=1;
    }
    int t=pow(8,x);
    int kk=0;
    for(int i=0;i<t;i++)
    {
        if(!vis[i]) 
        {
            int kk=x;
            while(x)
            {
                int tt=i%8;
                ans[x]=zimu[tt];
                i/=8;
                x--;
            }
            for(int j=1;j<=kk;j++)
                printf("%c",ans[j] );
            return 1;
        }
    }
    return 0;
}


int main()
{
    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        gets(s);
        len=strlen(s);
        for(int i=1;!solve(i);i++) ;
            printf("\n");
    }
}

相關推薦

hdu 4886 字串 hash+暴力

After improving the marketing strategy, TIANKENG has made a fortune and he is going to step into the status of TuHao. Nevertheless,

hdu 3973 字串hash+線段樹

Problem Description You are given some words {Wi}. Then our stupid AC will give you a very long string S. AC is stupid and always wan

hdu-1800(字串hash

題目連結:傳送門 思路: 就是找最多多少個掃帚,每個掃帚上有連續遞增的序列,就是找一個最多重複數字的重複次數。 由於是30位,每次用char*型別,然後用hash對映一下,排序找最多就行了。 注意: (1)num最小也是1。 (2)注意前導零。 #include<io

(通俗易懂小白入門)字串Hash+map判重——暴力且優雅

字串Hash 今天我們要講解的是用於處理字串匹配查重的一個演算法,當我們處理一些問題如給出10000個字串輸出其中不同的個數,或者給一個長度100000的字串,找出其中相同的字串有多少個(這樣描述有點不清楚但是大致的意思就是當字串長度很長,而且涉及到多個字串之間反覆比較時,由於比較的次數多,字串長,很容易就超

Hdu 1261字串

fine 結束 -- script scan input 測試 -1 return 字串數 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su

HDU-2089不要62-暴力或數位DP入門

之前 scanf href esp ostream += log main string 不要62 題意:給定區間,求在這個區間中有多少個數字,不包含4且不包含62; 這道題作為數位DP的入門題; 暴力也是可以過 #include<cstdio> #inclu

最長回文字串暴力

最長 pan 符號 bsp 多個 post gets getchar() %d 題目大意: 輸入一個字符串,求出其中最長的回文子串。子串的含義是:在原串中連續出現的字符串片段。回文的含義是:正著看和倒著看相同,如abba和yyxyy。在判斷時,忽略所有標點符號和空格,且忽略

[NOI2017]蚯蚓排隊 - hash + 暴力

style mod query void img i++ one tmp bre 經過一通復雜度分析後發現,只要暴力做就可以了 1 #include <iostream> 2 #include <cstdio> 3 #includ

hdu 6430 線段樹 暴力維護

mission ini common enc deep edge tree def acc Problem E. TeaTree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 524288/524288 K

hdu 1238 Substrings(暴力+string類方法)

題意:求n個字串的最長公共子串的長度,注意,公共子串翻轉後也算同一公共子串 題解:字串長度<=100,n<=100,t<=10,可以用暴力列舉,一開始我用map去記錄每一個字串的所有子串,最後比較一下最大值 發現超時了,後來才知道,可以以最小長度的那個字串來列舉所有字串

ural1989 單點更新+字串hash

正解是雙雜湊,不過一次雜湊也能解決。。 然後某個數字就對應一個字串,雖然有些不同串對應同一個數字,但是概率非常小,可以忽略不計。從左到右、從右到左進行兩次hash,如果是迴文串,那麼對應的整數必定存在某種關係(可以理解成相等),對於更新操作,就是單點更新。 #include<iostream&

HDU-4821-String-hash-map

title: HDU 4821 String --hash+map date: 2018-10-30 10:30:03 categories: “演算法” tag: - ACM - map - hash 題意: 給上限為1e5的字串,找出有多少長度為M*L的子串,並且該子串

Power Strings---字串hash

問題 C: 【雜湊和雜湊表】Power Strings 時間限制: 1 Sec  記憶體限制: 128 MB 提交: 38  解決: 18 [提交] [狀態] [討論版] [命題人:外部匯入] 題目描述 Gi

luogu4407 [JSOI2009]電子字典 字串hash + hash

暴力列舉,然後\(hash\)表判斷 複雜度\(O(26 * 20 * n)\) 具體而言 對於操作1:暴力列舉刪除 對於操作2:暴力新增,注意新增不要重複 對於操作3:暴力替換,同樣的注意不要重複 #include <cstdio> #include <cstr

C 語言 字串替換 暴力求解

#include<iostream> #include<stdio.h> using namespace std; int main() { int index=0; cout<<"Hello world!" << endl;

【水】HDU 2017 字串統計

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) [顯示標籤] Description 對於給定的一個字串,統計其中數字字元出現的次數。 Input 輸入資料有多

HDU 1264 Counting Squares (暴力水)

#include<bits/stdc++.h> using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #defi

洛谷P1032 字串變換 【kmp,字串hash

大意 給定轉換規則,求最小步數 思路 其實可以用AC自動機 這道題是問我們最小步數,因為其分支不大(≤7\leq7≤7)容易想到專門處理最優化問題的bfsbfsbfs演算法 在bfsbfsbfs的匹配中,本人採用的是用字元陣列模擬字串中的運算,建立新的“Str

985F Isomorphic Strings(字串Hash雜湊)

F. Isomorphic Strings time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard outpu

【資料結構】【線段樹】【字串Hash】2018國慶三校聯考D4T3

題意: 分析: 題解見標籤 (不過這題有非正解方法可以卡過去。。我程式碼附在下面) 正解: #include<cstdio> #include<cstring> #inclu