poj1023

二进制相关问题,构造

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<algorithm>
using namespace std;

#define LL long long
#define maxn 100

int n;
LL m;
char st[maxn];
int f[maxn];
int g[maxn];

bool work()
{
memset(f,
0, sizeof(f));
memset(g,
0, sizeof(g));
for (int i = 0; i < n; i++)
f[i]
= (m >> i) & 1;
for (int i = 0; i < n; i++)
{
if (!f[i])
{
g[i]
= 0;
continue;
}
if (f[i] && st[i] == 'p')
{
g[i]
= 1;
continue;
}
g[i]
= 1;
i
++;
while (i < n && !(st[i] == 'p' && f[i] == 0))
{
if (f[i])
g[i]
= 0;
else
g[i]
= 1;
i
++;
}
if (i >= n)
return false;
g[i]
= 1;
}
return true;
}

void print()
{
for (int i = n - 1; i >= 0; i--)
putchar(
'0' + g[i]);
putchar(
'\n');
}

int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf(
"%d", &t);
while (t--)
{
scanf(
"%d", &n);
scanf(
"%s", st);
reverse(st, st
+ n);
scanf(
"%lld", &m);
if (m < 0)
{
for (int i = 0; i < n; i++)
if (st[i] == 'p')
st[i]
= 'n';
else
st[i]
= 'p';
m
=-m;
}
if (!work())
printf(
"Impossible\n");
else
print();
}
return 0;
}
posted @ 2011-08-28 16:34  金海峰  阅读(247)  评论(0编辑  收藏  举报