Moderate simulated questions, various brute force Enumeration
Can be used to train code capabilities
View Code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct DD{
char s[25];
}dic[120];
int cmp(DD a,DD b){
return strcmp(a.s,b.s)<0;
}
char query[100];
char ss[100][100];
bool judge(char *query,char *dic)
{
char unsign[100];
char pos[100];
char neg[100];
memset(unsign,'\0',sizeof(unsign));
memset(pos,'\0',sizeof(pos));
memset(neg,'\0',sizeof(neg));
int uns=0,po=0,ne=0;
int len=strlen(query);
int i,j;
if(query[0]!='+'&&query[0]!='-') unsign[uns++]=query[0];
for(i=1;i<len;i++)
{
if(query[i-1]!='+'&&query[i-1]!='-'&&query[i]!='+'&&query[i]!='-')
{
unsign[uns++]=query[i];
}
}
for(i=0;i<len-1;i++)
{
if(query[i]=='+')
pos[po++]=query[i+1];
}
for(i=0;i<len-1;i++)
{
if(query[i]=='-')
neg[ne++]=query[i+1];
}
len=strlen(dic);
bool flag=false;
for(i=0;i<uns;i++)
{
for(j=0;j<len;j++)
{
if(dic[j]==unsign[i])
{
flag=true;
break;
}
}
if(flag) break;
}
if(!flag) return false;
for(i=0;i<po;i++)
{
flag=false;
for(j=0;j<len;j++)
{
if(dic[j]==pos[i])
flag=true;
}
if(!flag) break;
}
if(!flag) return false;
for(i=0;i<ne;i++)
{
flag=true;
for(j=0;j<len;j++)
{
if(dic[j]==neg[i])
{
flag=false;
break;
}
}
if(!flag) break;
}
if(!flag) return false;
return true;
}
int solve(char *s,int tot)
{
int i;
for(i=0;i<tot;i++)
{
if(judge(s,dic[i].s))
return i;
}
return -1;
}
int main()
{
int t,ca=1,i,j,k,n,m;
int tot;
char s[100];
while(scanf("%s",s)!=EOF)
{
tot=0;
if(s[0]=='#') break;
strcpy(dic[tot++].s,s);
while(scanf("%s",s)!=EOF)
{
if(s[0]=='*') break;
strcpy(dic[tot++].s,s);
}
sort(dic,dic+tot,cmp);
while(scanf("%s",s)!=EOF)
{
int nq=0,terms=0;
memset(query,'\0',sizeof(query));
memset(ss,'\0',sizeof(ss));
if(s[0]=='*'&&s[1]=='*') break;
int len=strlen(s);
for(i=0;i<len;i++)
{
if(s[i]!='|')
{
query[nq++]=s[i];
}
else
{
strcpy(ss[terms++],query);
nq=0;
memset(query,'\0',sizeof(query));
}
}
strcpy(ss[terms++],query);
int ans=10000000,id;
for(i=0;i<terms;i++)
{
int id=solve(ss[i],tot);
if(id!=-1&&id<ans)
{
ans=id;
}
}
if(ans!=10000000) printf("%s\n",dic[ans].s);
else printf("NONE\n");
}
printf("$\n");
}
return 0;
}