题目链接: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