ACM/ICPC 之 暴力打表(求解欧拉回路)-编码(POJ1780)

///找到一个数字序列包含所有n位数(连续)一次且仅一次
///暴力打表
///Time:141Ms Memory:2260K
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; #define MAX 1000010 bool v[MAX];
char num[6][MAX]; int main()
{
//freopen("in.txt", "r", stdin);
for(int t = 1; t <= 6; t++)
{
//初始化
memset(v, false, sizeof(v));
int len = t-1, mod = 1;
for(int i = 1; i <= t; i++)
mod *= 10; //mod:取模数
len += mod; //输出总数
for(int i = 0; i < t; i++)
num[t-1][i] = '0'; int cur = 0;
v[cur] = true;
for(int i = t; i < len; i++)
{
int j = 0; //向后一位试探0-9
for(; j < 10; j++)
{
int last = (cur * 10 + j) % mod;
if(!v[last]){ //标识
cur = last;
v[cur] = true;
break;
}
}
if(j == 10){ //试探未果
num[t-1][--i]++; //回推一位+1
v[cur] = false; //取消已有标识
while(num[t-1][i] == '0' + 10 || v[cur+1]) //超出十进制或已标识
{
if(num[t-1][i] != '0' + 10 && v[cur + 1]){ //仅仅已标识
num[t-1][i]++;
cur++;
continue;
}
i--; //否则继续回推+1
cur = ((num[t-1][i-t+1] - '0') * mod + cur)/10;
v[cur] = false;
num[t-1][i]++;
}
cur++;
continue;
}
else num[t-1][i] = '0' + j;
}
} int n;
while(scanf("%d", &n), n)
printf("%s\n", num[n-1]);
return 0;
}
上一篇:Friends number NBUT - 1223 (暴力打表)


下一篇:javascript中的后退和刷新