2014_GCJ_A
题目链接:http://code.google.com/codejam/contest/2984486/dashboard#s=p0
最想吐槽的是想些DFS过小数据,居然写不出来,不知道我这半年的ACM生活在干嘛?
先不说直接出最有解,但是DFS这基本的还不会,真是,估计快废了!
DFS过小数据的程序:
#include<iostream>//又臭又长,无语
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<set>
#include<map>
#include<cstdio>
int n,l;
int ans;
using namespace std;
string s[1000],s1[1000],tem[1000];
int b[1000];
int pan()
{
sort(tem+1,tem+n+1);
for (int i=1;i<=n;i++)
if (s1[i]!=tem[i]) return 0;
return 1;
}
void res(int x)
{
for (int i=1;i<=n;i++)
tem[i][x]^=1;//PS这里注意一下,字符为‘0’,或者‘1’;可以算出字符'0'^1='1','1'^1='0';
}
void rec(int x)
{
for (int i=1;i<=n;i++)
tem[i][x]^=1;
}
void dfs(int t,int k)
{
if (t==l)
{
if (pan()) ans=min(ans,k);return; }
dfs(t+1,k);
res(t);
dfs(t+1,k+1);
rec(t);
}
int main()
{
int t;
freopen("A-small-practice.in","r",stdin);
freopen("out.txt","w",stdout);
cin>>t;
for (int o=1;o<=t;o++){
ans=999999;
memset(b,0,sizeof(b));
cout<<"Case #"<<o<<": ";
cin>>n>>l;
for (int i=1;i<=n;i++) {cin>>s[i];tem[i]=s[i];}
for (int i=1;i<=n;i++) cin>>s1[i];
sort(s1+1,s1+n+1);
dfs(0,0);
if (ans<999999)
cout<<ans<<endl;
else cout<<"NOT POSSIBLE"<<endl;
}
return 0;
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<set>
#include<map>
#include<cstdio>
int n,l;
int ans;
using namespace std;
string s[1000],s1[1000],tem[1000];
int b[1000];
int pan()
{
sort(tem+1,tem+n+1);
for (int i=1;i<=n;i++)
if (s1[i]!=tem[i]) return 0;
return 1;
}
void res(int x)
{
for (int i=1;i<=n;i++)
tem[i][x]^=1;//PS这里注意一下,字符为‘0’,或者‘1’;可以算出字符'0'^1='1','1'^1='0';
}
void rec(int x)
{
for (int i=1;i<=n;i++)
tem[i][x]^=1;
}
void dfs(int t,int k)
{
if (t==l)
{
if (pan()) ans=min(ans,k);return; }
dfs(t+1,k);
res(t);
dfs(t+1,k+1);
rec(t);
}
int main()
{
int t;
freopen("A-small-practice.in","r",stdin);
freopen("out.txt","w",stdout);
cin>>t;
for (int o=1;o<=t;o++){
ans=999999;
memset(b,0,sizeof(b));
cout<<"Case #"<<o<<": ";
cin>>n>>l;
for (int i=1;i<=n;i++) {cin>>s[i];tem[i]=s[i];}
for (int i=1;i<=n;i++) cin>>s1[i];
sort(s1+1,s1+n+1);
dfs(0,0);
if (ans<999999)
cout<<ans<<endl;
else cout<<"NOT POSSIBLE"<<endl;
}
return 0;
}
正解是:枚举第I个数,因为存在解的话肯定是A中第I个数与B第一个数相同,然后根据两者数字的不同去跟新其他位数上的数!复杂度大概是O(N*N*L);
部分代码: for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++) ss[j]=s[j];
int now=0;
for (int j=0;j<l;j++)
if (ss[1][j]!=s1[i][j]){
for (int k=1;k<=n;k++)
ss[k][j]^=1;
now++;
}
sort(ss+1,ss+n+1);
int flag=0;
for (int k=1;k<=n;k++)
if (ss[k]!=s1[k]){flag=1;break;}
if (!flag) ans=min(ans,now);
}
int now=0;
for (int j=0;j<l;j++)
if (ss[1][j]!=s1[i][j]){
for (int k=1;k<=n;k++)
ss[k][j]^=1;
now++;
}
sort(ss+1,ss+n+1);
int flag=0;
for (int k=1;k<=n;k++)
if (ss[k]!=s1[k]){flag=1;break;}
if (!flag) ans=min(ans,now);
}
等下再做做位运算
随性Code