类似于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. Running the app on your device

    So far, you've run the app on the Simulator. That's nice and all but probably notwhy you're learning ...

  2. Makefile中怎样调用python和perl文件为自己提供须要的数据

    Makefile中怎样调用python和perl文件为自己提供须要的数据,利用print函数对外输出数据 实例代码例如以下 perl.pl #!/usr/bin/perl print("he ...

  3. spring的bean管理(注解和配置文件混合使用)

    1.建三个类,在一个类中引用其他两个类 import javax.annotation.Resource; import org.springframework.beans.factory.annot ...

  4. Swift - 将Data数据转换为[UInt8](bytes字节数组)

    有时上传或者发送图片.文字时,需要将数据转换为 bytes 字节数组.下面介绍两种将 Data 转换为 [UInt8] 的方法. 假设我们有如下 Data 数据要转换: 1 let data = &q ...

  5. 国内物联网平台初探(六) ——庆科云FogCloud

    平台定位 FogCloud 快速接入智能硬件 FogCloud为开发者提供便捷的智能硬件接入服务,真正实现敏捷开发,快速迭代. FogCloud提供功能强大的云端服务 包括 产品/APP管理 ,消息通 ...

  6. Serializable-源码分析

    package java.io; public interface Serializable { } 代码很简单,功能也很简单,对象通过这个接口来实现序列化和反序列的.下面来看看小例子. import ...

  7. centos6.5 + Nat网络模式 +SecureCRT 的相关设置

    步骤1:先去查看子网掩码和子网ip 提示:打开后先不要关闭,后边还会使用 步骤2:查看本机名 输入: hostname 步骤3:修改本机名 vi /etc/sysconfig/network 在”Ho ...

  8. 第5章分布式系统模式 在 .NET 中使用 DataSet 实现 Data Transfer Object

    要在 .NET Framework 中实现分布式应用程序.客户端应用程序需要显示一个窗体,该窗体要求对 ASP.NET Web Service 进行多个调用以满足单个用户请求.基于性能方面的考虑,我们 ...

  9. python3 常用模块详解

    这里是python3的一些常用模块的用法详解,大家可以在这里找到它们. Python3 循环语句 python中模块sys与os的一些常用方法 Python3字符串 详解 Python3之时间模块详述 ...

  10. 最简单的一致性Hash算法实现

    import java.util.Collection;import java.util.SortedMap;import java.util.TreeMap; public class Consis ...