看懂题就不难写了~
#include<bits/stdc++.h>
using namespace std;
struct dat_edge
{
int aim,last;
}edge[201];
int n,m,k,inde[101],coun[101],tmp_coun[101],pl[101],len_edge,num_key,key[300][101];
char kind[101];
void insert_edge(int x,int y)
{
len_edge++;
edge[len_edge].aim=y;
edge[len_edge].last=pl[x];
pl[x]=len_edge;
}
void init(int o,string z)
{
int p,i,num;
string t;
p=1;
z+=' ';
for(i=1;i<z.length();i++)
if(z[i]==' ')
{
t=z.substr(p,i-p);
if(t[0]=='i')
num=0;
else
num=n;
if(t.length()==2)
num+=t[1]-'0';
else
{
num+=(t[1]-'0')*10+t[2]-'0';
}
coun[o]++;
insert_edge(num,o);
p=i+1;
}
}
bool solve(int key[],int poi,int sta)
{
int i,p,o,w,tmp,que[101],f[101];
for(i=1;i<=n+m+k;i++)
{
f[i]=0;
}
for(i=1;i<=n;i++)
f[i]=key[i];
for(i=1;i<=m;i++)
{
if(kind[i]=='a')
f[i+n]=1;
else
f[i+n]=0;
}
o=n;w=0;
for(i=1;i<=n;i++)
que[i]=i;
while(w<o)
{
w++;
if(que[w]==poi)
{
if(sta==2)
f[que[w]]=!f[que[w]];
else
f[que[w]]=sta;
}
p=pl[que[w]];
while(p!=-1)
{
coun[edge[p].aim]--;
if(coun[edge[p].aim]==0)
{
que[++o]=edge[p].aim;
}
tmp=edge[p].aim;
if(kind[tmp-n]=='a')
{
if(f[que[w]]==0)
f[tmp]=0;
}
else if(kind[tmp-n]=='o')
{
if(f[que[w]]==1)
f[tmp]=1;
}
else if(kind[tmp-n]=='x')
{
f[tmp]=f[tmp]^f[que[w]];
}
else
{
f[tmp]=!f[que[w]];
}
p=edge[p].last;
}
}
for(i=1;i<=n+m+k;i++)
coun[i]=tmp_coun[i];
for(i=1;i<=k;i++)
{
if(f[inde[i]+n]!=key[n+i])
return false;
}
return true;
}
void work(int t)
{
int i,j,p,ans1,ans2,coun_ans;
bool ok=true;
for(i=1;i<=num_key;i++)
{
if(!solve(key[i],-1,-1))
{
ok=false;
break;
}
}
if(ok)
{
cout<<"Case "<<t<<": No faults detected\n";
return;
}
ans1=ans2=coun_ans=0;
for(i=1;i<=m;i++)
{
for(j=0;j<=2;j++)
{
ok=true;
for(p=1;p<=num_key;p++)
{
ok=solve(key[p],i+n,j);
if(!ok)
break;
}
if(ok)
{
ans1=i;
ans2=j;
coun_ans++;
}
}
}
if(coun_ans!=1)
{
cout<<"Case "<<t<<": Unable to totally classify the failure\n";
return;
}
if(ans2==2)
{
cout<<"Case "<<t<<": Gate "<<ans1<<" is failing; output inverted\n";
return;
}
if(ans2==0)
{
cout<<"Case "<<t<<": Gate "<<ans1<<" is failing; output stuck at 0\n";
return;
}
if(ans2==1)
{
cout<<"Case "<<t<<": Gate "<<ans1<<" is failing; output stuck at 1\n";
return;
}
}
int main()
{
int t,i,j;
string z;
t=0;
while(cin>>n>>m>>k && n+m+k!=0)
{
t++;
memset(coun,0,sizeof(coun));
len_edge=-1;
for(i=1;i<=n+m+k;i++)
pl[i]=-1;
for(i=1;i<=m;i++)
{
cin>>kind[i];
getline(cin,z);
init(i+n,z);
}
for(i=1;i<=n+m+k;i++)
tmp_coun[i]=coun[i];
for(i=1;i<=k;i++)
cin>>inde[i];
cin>>num_key;
for(i=1;i<=num_key;i++)
for(j=1;j<=n+k;j++)
cin>>key[i][j];
work(t);
}
return 0;
}