题意:
一个字符串有许多子串 现要找出最短的字典序最小的不是它的子串的串 这个长串只有A~H字母
思路:
YY一下答案串能有多长 8^7就比长串长了 所以也就是7的长度
那么只需要枚举长度 利用哈希判定字符串出现的问题 如何哈希呢?
一共就8个字母明显搞成8进制数 例如 AABCAD 就是 001203(8) 只有7的长度连int都不会爆 哈希稳稳的
而且通过hash值可以很简单的转化回字符串 输出也方便
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1000010
char str[N];
int hash[N], num[N];
int ans, end;
int main() {
int t, i, j, len;
scanf("%d", &t);
while (t--) {
scanf("%s", str + 1);
len = strlen(str + 1);
memset(hash, 0, sizeof(hash));
memset(num, 0, sizeof(num));
end = 8;
for (j = 1; j <= 7; j++) {
for (i = len; i >= j; i--) {
hash[i] = hash[i - 1] * 8 + str[i] - 'A';
num[hash[i]]++;
}
for (i = 0; i < end; i++) {
if (!num[i]) {
ans = i;
goto FZC;
}
num[i] = 0;
}
end *= 8;
}
FZC: i = 0;
while (j--) {
str[i++] = ans % 8 + 'A';
ans /= 8;
}
for (j = i - 1; j >= 0; j--)
putchar(str[j]);
puts("");
}
return 0;
}
HDU 4886 TIANKENG’s restaurant(Ⅱ),布布扣,bubuko.com
HDU 4886 TIANKENG’s restaurant(Ⅱ)
原文:http://blog.csdn.net/houserabbit/article/details/38278367