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