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");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
字符串哈希滑动窗口是一种用于处理字符串的算法。它主要用于在给定的字符串中找到满足特定条件的子串。 在字符串哈希滑动窗口算法中,我们首先计算原始字符串的哈希值。然后,我们使用一个滑动窗口来遍历字符串,每次滑动一个固定长度的窗口。我们可以通过比较每个窗口内的子串的哈希值来判断是否满足条件。 具体而言,我们可以使用BKDRHash等哈希函数来计算字符串的哈希值。然后,我们枚举每个可能的起点,并使用滑动窗口来计算窗口内的子串的哈希值。通过比较窗口内的子串的哈希值,我们可以判断是否满足条件。 对于滑动窗口的移动,如果窗口内的子串满足条件,我们可以继续将窗口往右移动一个固定的长度。如果窗口内的子串不满足条件,我们将窗口的右边界移到最右端,并依次比较新窗口内的子串的哈希值。 综上所述,字符串哈希滑动窗口算法是通过计算字符串的哈希值,并使用滑动窗口来遍历字符串,以找到满足特定条件的子串。这个算法可以高效地处理字符串,并且能够应用于各种字符串相关的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [String (字符串哈希+滑动窗口)](https://blog.csdn.net/weixin_43872264/article/details/107571742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【字符串hash+滑动窗口】String HDU - 4821](https://blog.csdn.net/qq_45599865/article/details/111143633)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值