HDU 4886 tiankeng's restaurant (ⅱ)

Source: Internet
Author: User

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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.