类似于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. 2016.04.13,英语,《Vocabulary Builder》Unit 13

    cord, from the Latin word for 'heart'. concord, ['kɑːŋkɔːrd] n. 和睦, 公约 con-,'with'. discord, ['dɪskɔ ...

  2. xcode打包测试

    模拟器的内存cpu网络,都是电脑的.xcode可以查看. Xcode7之前是限制人,限制电脑,限制app,限制真机调试的. Xcode7之后,做真机测试只需要apple id即可,会自动生成证书. X ...

  3. PowerShell攻防进阶篇:nishang工具用法详解

    PowerShell攻防进阶篇:nishang工具用法详解 导语:nishang,PowerShell下并肩Empire,Powersploit的神器. 开始之前,先放出个下载地址! 下载地址:htt ...

  4. EOJ 2822 内存显示

    一个 int 类型变量或 double 类型变量在连续几个字节的内存中存放.读取数值时,当数值中包含小数点时类型为 double,否则类型为 int.将读入的数值存放在 int 类型变量或 doubl ...

  5. TF-IDF算法--关键词句和文本集中每篇文章相关度计算

    关键词句和文本集每篇文章相关度计算:假设语料库中有几万篇文章,每篇文章的长度不一,你任意输入关键词或句子,通过代码以tf-idf值为准检索出来相似度高的文章. 1.TF-IDF概述 TF-IDF是一种 ...

  6. codeforces 712 Memory and De-Evolution

    2019-05-19 13:25:37 加油,加油,坚持,坚持!!! 一定要逆推,才可以是最少的次数 更好的方法: https://www.cnblogs.com/ECJTUACM-873284962 ...

  7. Redis(三)、Redis主从复制

    一.主从复制 主从复制:主节点负责写数据,从节点负责读数据,从而实现读写分离,提高redis的高可用性. 让一个服务器去复制(replicate)另一个服务器,我们称呼被复制的服务器为主节点(mast ...

  8. 如何在Hexo中实现自适应响应式相册功能

    用最清晰简洁的方法整合一个响应式相册 效果 技术选型 由于我选用的主题使用了fancyBox作为图片弹出展示的框架,查看后表示很不错,能满足需要 http://fancyapps.com/fancyb ...

  9. vuex的状态管理模式

    1.store.js Vuex 通过 store 选项,提供了一种机制将状态从根组件“注入”到每一个子组件中(需调用 Vue.use(Vuex)) state:存放数据. mutations:提交状态 ...

  10. SQL Server-聚焦过滤索引提高查询性能

    前言 这一节我们还是继续讲讲索引知识,前面我们讲了聚集索引.非聚集索引以及覆盖索引等,在这其中还有一个过滤索引,通过索引过滤我们也能提高查询性能,简短的内容,深入的理解,Always to revie ...