題意:長m的01環,每一個長k的子串都是不一樣的01串。給出k,求最大的M以及字典序最小的方案。php
\(M=2^k\)ios
能夠把k-1位01串當作點,k位01串就是邊,知足歐拉回路的條件。spa
而後求字典序最小的歐拉回路就好了,優先走字典序小的邊code
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <set> #include <map> using namespace std; typedef long long ll; #define fir first #define sec second const int N = (1<<11)+5, inf = 1e9+5; inline int read() { char c=getchar(); int x=0,f=1; while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} return x*f; } int m, k, mark[N], st[N], top, c; void dfs(int s) { for(int i=0; i<2; i++) { int t = (s<<1) | i; if(mark[t]) continue; mark[t] = 1; dfs(t & c); st[++top] = t; } } int main() { k = read(); m = 1<<k; printf("%d ", m); for(int i=0; i<k-1; i++) c |= (1<<i); dfs(0); for(int i=0; i<k; i++) printf("%d", st[top] & (1<<i)); top--; while(top >= k) printf("%d", st[top--] & 1); }