类似于DP一样做,但这题有个大坑,自己看DIS吧。。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string.h>
#define LL __int64
using namespace std; const int N=1<<16;
const int inf=1<<30;
int dp[N];
char str[25];
int ans[25];
int num[125],b,n,goal; int judge(int f,int s){
int tmp;int c=0;
for(int k=0;k<b;k++){
tmp=1<<k;
if((f&tmp)==(s&tmp))
c++;
}
return c;
} int main(){
while(scanf("%d%d",&b,&n)!=EOF){
scanf("%s",str+1);
goal=0;
int len=strlen(str+1);
for(int i=len;i>=1;i--){
int t=str[i]-'0';
goal+=t*(1<<(b-i));
}
int tmp=(1<<b)-1;;
for(int k=1;k<=n;k++){
scanf("%s",str+1);
len=strlen(str+1);
num[k]=0;
for(int i=len;i>=1;i--){
int t=str[i]-'0';
num[k]+=t*(1<<(b-i));
}
}
bool flag=false;
for(int k=1;k<=n;k++){
if(num[k]==goal){
len=0;
if(goal==0){
printf("1\n");
int pos=goal;
while(b--){
ans[++len]=pos%2;
pos>>=1;
}
for(int i=len;i>0;i--)
printf("%d",ans[i]);
printf("\n");
flag=true;
break;
}
else if(goal!=0){
printf("2\n");
int pos=goal;
while(b--){
ans[++len]=pos%2;
pos>>=1;
}
for(int i=len;i>0;i--)
printf("%d",ans[i]);
printf("\n");
flag=true;
break;
}
}
}
if(flag) continue;
for(int k=0;k<=tmp;k++){
dp[k]=inf;
}
for(int i=1;i<=n;i++)
dp[num[i]]=0;
for(int i=1;i<=n;i++){
for(int k=0;k<=tmp;k++){
dp[k]=min(dp[k],dp[k^num[i]]+1);
}
}
int pos=-1,c=-1,t;
for(int k=0;k<=tmp;k++){
if(dp[k]!=inf){
t=judge(k,goal);
if(t>c){
pos=k;
c=t;
}
else if(t==c){
if(dp[pos]>dp[k])
pos=k;
}
}
}
printf("%d\n",dp[pos]);
len=0;
while(b--){
ans[++len]=pos%2;
pos>>=1;
}
for(int i=len;i>0;i--)
printf("%d",ans[i]);
printf("\n");
}
return 0;
}

  

POJ 2133的更多相关文章

  1. POJ 2133 暴搜

    题意: 思路: 按照题意暴搜 注意 如果目标串==给的串 答案是2 //By SiriurRen #include <cstdio> #include <cstring> #i ...

  2. HOJ 2133&POJ 2964 Tourist(动态规划)

    Tourist Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1503 Accepted: 617 Description A ...

  3. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  4. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  5. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  6. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  7. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  8. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  9. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

随机推荐

  1. codetemplate

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><templa ...

  2. nyoj--127--星际之门(一)(生成树的数量)

    星际之门(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 公元3000年,子虚帝国统领着N个星系,原先它们是靠近光束飞船来进行旅行的,近来,X博士发明了星际之门,它 ...

  3. nyoj--745--蚂蚁的难题(二)

    蚂蚁的难题(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 下雨了,下雨了,蚂蚁搬家了. 已知有n种食材需要搬走,这些食材从1到n依次排成了一个圈.小蚂蚁对每种食材 ...

  4. 2017-3-9 leetcode 283 287 289

    今天操作系统课,没能安心睡懒觉23333,妹抖龙更新,可惜感觉水分不少....怀念追RE0的感觉 =================================================== ...

  5. C#关于XML的一些简单用法

    关于XML文件的用法,本文简单介绍创建.读取和增删操作 . 1.创建有三种方法 (1)通过XmlDocument创建,然后分级添加子目录 XmlDocument doc = new XmlDocume ...

  6. ps -aux ,ps aux ,ps -ef 的区别

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  7. My first blog for java

    我的第一个java程序: package com.hellojava; /** * @author 沽-名-钓-誉 */ public class HelloJava{ /** * @param 输出 ...

  8. 利用JavaScript的%读分秒

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 有关Gradle Network is unreachable: connect的报错

    项目Gradle   Errer:Network is unreachable: connect 同时还有as的 报错 Internal HTTP server disabled: Cannot st ...

  10. 读书笔记6-浪潮之巅(part1)

    浪潮之巅 ——对于一个人来讲,一生能够赶上一次科技革命的浪潮也就足够了 近一百多年来,总有一些公司很幸运地站在了技术革命的浪尖上.而一旦处在那个位置,就算只用随着潮流的发展而前行,也能安安稳稳地发展十 ...