Hdu 4850 Wow! Such String! (Euler loop), hdu4850

Source: Internet
Author: User

Hdu 4850 Wow! Such String! (Euler loop), hdu4850

Link: hdu 4850 Wow! Such String!

Given an n, a string with a length of n must be output, and there will be no repeated substrings with a length greater than or equal to 4. impossible output cannot be obtained.

Solution: This question is misleading. In fact, 500000 is not constructed so long. We consider all strings with different lengths and 4, a total of 264 s =, assume that there is a long string that does not have repeated substrings with a length greater than or equal to 4. Then, use 0, 1, 2, 3... the starting position is the starting position. The substring with the length of 4 must be a string in s. Because it is not repeated, only 264 positions must be used as the starting point, and the ending three characters must be added, A total of 264 + 3 characters can be used to construct a condition string.
The constructor is actually an Euler loop, with a 3-character string as the node. There are a total of 263 nodes, with each node having a outbound degree of 26 and an inbound degree of 26, the starting point of the final constructed string is the weight of the walking edge, requiring each node to go through exactly 26 times. I am using a greedy method. The next node that can be moved each time must be the node that can be moved to the node for the minimum number of times. This ensures that each node can go exactly 26 times.

Note: because it is an aaa node at the beginning, for each node, the side with the weight of a should be considered only when there is no way to go, because this graph is an Euler loop, that is, to answer the aaa point at last, if the edge of weight a is considered during the process, the aaa point may be taken over once during the process, and the last path will be blocked. Therefore, you must leave the back side.

#include <cstdio>#include <cstring>#include <set>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 26*26*26;const int mod = 26 * 26;const int maxl = 500005;int maxlen;int v[maxn+5][30], c[maxn+5];char s[maxl];inline int get_next (int u, int k) {    return (u % mod) * 26 + k;}int find_next (int u) {    int x = 0, ans = -1;    for (int i = 1; i < 26; i++) {        if (v[u][i])            continue;        int tmp = get_next(u, i);        if (ans == -1 || c[tmp] < c[ans]) {            ans = tmp;            x = i;        }    }    return x;}void init () {    maxlen = maxn * 26 + 3;    int mv, u = 0;    memset(v, 0, sizeof(v));    for (mv = 0; mv < 3; mv++)        s[mv] = 'a';    while (true) {        int x = find_next(u);        int next = get_next(u, x);        if (c[next] == 26)            break;        c[next]++;        s[mv++] = x + 'a';        v[u][x] = 1;        u = next;    }    /*    for (int i = 0; i < maxn; i++) {        bool flag = false;;        for (int j = 0; j < 26; j++)            if (v[i][j] == 0)                flag = true;        if (flag)            printf("%d\n", i);    }    printf("%d %d\n", mv, maxlen);    */}int main () {    int n;    init();    while (scanf("%d", &n) == 1) {        if (n <= maxlen) {            for (int i = 0; i < n; i++)                printf("%c", s[i]);            printf("\n");        } else            printf("Impossible\n");    }    return 0;}

How to Establish Euler's loop

Program EulerPath_Algorithm;
(*************************************** ***********************************}
{An Euler path (pronounced 'tailer') in a graph G is a path that uses each}
{Arc of G exactly once and can exist in a connected graph iff there are}
{Either no nodes whose degree is odd or exactly two nodes whose degree is}
{Odd. For the case of no nodes posessing an odd degree, the path can}
{Begin at any node and will end there; for the case of two nodes posessing}
{An odd degree, the path must begin at one odd node and end at the other .}
{--------------------------------------------------------------------------}
{Released to the public domain by Natural Systems. This routine may be}
{Freely distributed .}
{--------------------------------------------------------------------------}
{Natural Systems is the producer of data structures (DS), a highly}
{Versatelibrary of the most critical structures -- such as graphs --}
{All Borland compilers supporting OOP .}
{}
{Euler's, as well as the Dijkstra, Warshall, Bellman-Ford, Floyd's ,}
{Kruskal's (and found, more) methods, are available in DS. And}
{That's just one class -- there's too failed to list... (Of course, because}
{Of its 'object oriented nature, there are no restri ...... the remaining full text >>>
 
Urgent c ++ fleury algorithm Euler Loop code

1 # include <stdio. h>
2 # include <string. h>
3
4
5 struct stack
6 {int top, node [210] ;}f; // vertex Stack
7
8int a [201] [201]; // The adjacent matrix of the graph
9
10int n;
11
12 void dfs (int x) // depth-first traversal of the graph
13 {
14int I;
15
16f. top ++; f. node [f. top] = x;
17
18for (I = 1; I <= n; I ++)
19
20 if (a [I] [x]> 0)
21 {
22 a [I] [x] = 0; a [x] [I] = 0; // Delete this edge
23
24 dfs (I );
25
26 break;
27}
28}
29
30 void Euler (int x) // Euler's Algorithm
31 {
32int I, B;
33
34f. top = 0; f. node [f. top] = x; // inbound Stack
35
36 while (f. top> = 0)
37 {
38 B = 0;
39
40 for (I = 1; I <= n; I ++)
41 if (a [f. node [f. top] [I]> 0)
42 {B = 1; break ;}
43
44 if (B = 0) // if there is no point, it can be expanded, output and Output
45 {
46 printf ("% d", f. node [f. top]);
47
48 f. top --;
49}
50 else {f. top --; dfs (f. node [f. top + 1]);} // If yes, DFS
51}
52}
53
54int main ()
55 {
56
57int m, s, t, num, I, j, start;
58
59 // input
60
61 scanf ("% d", & n, & m); // n vertex count m edge count
62
63 memset (a, 0, sizeof ());
64
65 for (I = 0; I <m; I ++)
66 {
67 scanf ("% d", & s, & t );
68 a [s] [t] = 1; a [t] [s] = 1;
69}
70
71
72 // determine whether there is an Euler Loop
73
74 s = 0; start = 1;
75
76 for (I = 1; I <= n; I ++)
77 {
78 num = 0;
79
80 ...... remaining full text>

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.