码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-28 15:49:13      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   io   for   ar   line   

题目链接: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+暴力),布布扣,bubuko.com

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

标签:style   http   color   os   io   for   ar   line   

原文地址:http://blog.csdn.net/keshuai19940722/article/details/38229671

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!