Question:
A string has many substrings. Now we need to find the smallest Lexicographic Order. The substring is not its substring. This long string is only ~ H letter
Ideas:
YY: How long is the answer string? 8 ^ 7 is longer than the long string, so it is the length of 7.
So we only need to enumerate the length and use hash to determine how to hash the character string?
For example, aabcad is a 001203 (8) 8-digit number with a length of only 7. Even int, the hash is stable.
In addition, the hash value can be easily converted back to the string for output.
Code:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 1000010char 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;}