POJ 2133
类似于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的更多相关文章
- POJ 2133 暴搜
题意: 思路: 按照题意暴搜 注意 如果目标串==给的串 答案是2 //By SiriurRen #include <cstdio> #include <cstring> #i ...
- HOJ 2133&POJ 2964 Tourist(动态规划)
Tourist Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1503 Accepted: 617 Description A ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
随机推荐
- Makefileeasy犯错的语法
1.引言 近期学习android的Build系统,接触最多的自然就是Makefile语法.发现非常多easy出错的地方,不避开这些错误语法没法真正了解Makefile的内涵.以下就介绍遇到的一些让人困 ...
- Python 字典(dict)操作(update)
1. get 注意以下两种形式的细微差别,差别在返回值的类型上: d.get(value, '') d.get(value, ['']) >> d = {} >> d.get( ...
- Epos消费管理系统复制迁移SQL SERVER 2005数据库
先脱机 原来要关闭Epos消费管理系统软件才可以让对应的数据库脱机
- maven 打包jar && lib
一.springboot 打包成jar 1.pom.xml <build> <!-- jar的名称--> <finalName>shiro</finalNam ...
- Python迭代与递归方法实现斐波拉契数列
首先是迭代的方法实现: def f(n): f1, f2, fn = 1, 1, 1 while n>2: fn = f1 + f2 f1 = f2 f2 = fn n = n - 1 retu ...
- Pop3协议详解
POP3全称为Post Office Protocol version3,即邮局协议第3版.它被用户代理用来邮件服务器取得邮件.POP3采用的也是C/S通信 模型 用户从邮件服务器上接收邮件的典型 ...
- 同一sql程序执行比数据库执行慢
最近项目发现同一个sql在java端执行比在数据库执行慢很多,原因可能是程序的sql参数类型与数据库字段的类型不一致.
- ansible upload
# 链接地址:https://www.cnblogs.com/xiaoxiaoleo/p/6626299.html # synchronize: 从拉取远程服务器文件,需要加mode: pull # ...
- Web-数据库-mysql篇
DDL创造 create 修改 alter 删除 dorp DML插入 insert 删除 delete 更新 updateDQLselect from where MySQL与JDBC 一.用 ...
- javascript 的逻辑中断(短路操作)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...