Code 欧拉通路 + 搜索
密码问题。密码为n位数字,求出 长度最短的包含每个n位密码的数字串。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <stack> 11 #include <queue> 12 #include <sstream> 13 #include <iomanip> 14 using namespace std; 15 typedef long long LL; 16 const int INF=0x4fffffff; 17 const int EXP=1e-5; 18 const int MS=100005; 19 const int SIZE=26; 20 21 int last[MS]; // 对于n-1位的值v,后面添加的0,1,2……9, 22 23 int sta[10*MS]; //数组模拟stack 24 25 char ans[10*MS]; 26 int size1,size2; 27 28 void search(int v,int m) 29 { 30 int w; 31 while(last[v]<10) 32 { 33 w=v*10+last[v]; 34 last[v]++; 35 sta[size1++]=w; 36 v=w%m; 37 } 38 } 39 40 int main() 41 { 42 int n,m,i,v; 43 while(scanf("%d",&n)&&n) 44 { 45 if(n==1) 46 { 47 printf("0123456789\n"); 48 continue; 49 } 50 size1=0; 51 size2=0; 52 v=0; 53 m=(int)(pow(10.0,double(n-1))+EXP); 54 55 for(int i=0;i<m;i++) 56 last[i]=0; 57 search(v,m); 58 while(size1) 59 { 60 v=sta[--size1]; 61 ans[size2++]=v%10+'0'; 62 v/=10; 63 search(v,m); 64 } 65 for(int i=1;i<n;i++) // 输出 000000这样的密码的前n-1个0 66 printf("0"); 67 while(size2) 68 printf("%c",ans[--size2]); 69 printf("\n"); 70 } 71 return 0; 72 }