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;

int n, f[10], vis[maxn];
char str[maxn];

bool solve (int k) {
    memset(vis, 0, sizeof(vis));
    int tmp = 0;
    for (int i = 0; i < k; i++)
        tmp = tmp * 8 + str[i] - ‘A‘;

    vis[tmp] = 1;
    for (int i = k; i < n; i++) {
        tmp = tmp % f[k-1];
        tmp = tmp * 8 + str[i] - ‘A‘;
        vis[tmp] = 1;
    }

    int x = 0;
    while (vis[x]) x++;

    if (x < f[k]) {
        for (int i = k-1; i >= 0; i--) {
            printf("%c", x / f[i] + ‘A‘);
            x %= f[i];
        }
        printf("\n");
        return true;
    }
    return false;
}

int main () {
    int cas;
    scanf("%d", &cas);
    f[0] = 1;
    for (int i = 1; i <= 8; i++)
        f[i] = f[i-1] * 8;

    while (cas--) {
        scanf("%s", str);
        n = strlen(str);
        for (int i = 1; !solve(i); i++);
    }
    return 0;
}

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

时间: 2024-10-16 07:32:35

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

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 )

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 fort

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