HDU 4850 wow! Such string! (Euler's Road)

Source: Internet
Author: User
HDU 4850 wow! Such string!

Question Link

Question: Ask for a string within 50 W, and ask for a substring with a length greater than or equal to 4 to appear only once.

Train of Thought: reasoning is required. There are a total of 26 ^ 4 types of 4-letter strings. These strings can be changed to another string if one character is added at the end of a string, as if there is an edge, each character has one node more than one, then for this question, there will be a total of 26 ^ 4 + 3 sides, with three more tails added, A total of 26 ^ 4 + 3 edges. All these edges are required strings, so that you can know that each node will pass through 26 times, in this way, you only need to consider how to concatenate these nodes to form an Euler's path. This is the biggest case. Select a starting point AAA and continue to move forward, each time you select the least occupied node to go, the edge is marked out, and then you need to note that it is because it starts from AAA and must return to AAA, therefore, we need to minimize the priority of edge A. Otherwise, it cannot be formed if it is occupied first.

Code:

#include <cstdio>#include <cstring>const int N = 20005;int vis[N], vis2[N][30], on = 0;char out[500005];int getnext(int x, int a) {    return x % (26 * 26) * 26 + a;}void init() {    int now = 0;    for (int i = 0; i < 3; i++)out[on++] = 'a';    while (true) {int Min = 26, iv = 0;for (int i = 1; i < 26; i++) {    if (vis2[now][i]) continue;    int tmp = getnext(now, i);    if (vis[tmp] < Min) {Min = vis[tmp];iv = i;    }}int tmp = getnext(now, iv);if (vis[tmp] == 26) break;vis2[now][iv] = 1;now = tmp;vis[now]++;out[on++] = now % 26 + 'a';    }}int n;int main() {    init();    while (~scanf("%d", &n)) {if (n > 456979) printf("Impossible\n");else printf("%s\n", (out + 456979 - n));    }    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.