题目链接:【HDU 4096】
输入t组数据,每组数据由无数个语句组成,当遇到‘!’时跳出循环,继续下一组数据的测试,一共有8种语句类型
1、名词 are 名词.
2、名词 can 动词.
3、everything which can 动词 can 动词.
4、everything which can 动词 are 名词.
5、are 名词 名词?
6、can 名词 动词?
7、can everything which can 动词 动词?
8、are everything which can 动词 名词?
从陈述句中我们可以得到各个词语之间的关系,遇到问句,如果这个问句成立,就输出Y,否则输出M
这题wa了一下午,最主要的坑点就是第5种句型跟第7种句型,只要前后两个词语一样,无论这两个词语是否出现过,都应该输出Y
(1) 用gets输入,用sscanf函数将每个单词保存下来,遇到一个单词就用map标记了
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <vector>
using namespace std;
vector<int>vec[300];
map<string, int>mpn, mpv;
char s1[200], s[10][20];
int vis[300];
string a, b;
bool dfs(int x, int y)
{
vis[x]=1;
if(x==y) return true;//没有特判,一开始我把它写到for循环里面了,一直wa
for(int i=0; i<vec[x].size(); i++)
{
int v = vec[x][i];
if(!vis[v])
{
if(dfs(v, y)) return true;
}
}
return false;
}
int main()
{
int t, cas=1;
scanf("%d", &t);
gets(s1);
while(t--)
{
for(int i=0;i<250; i++) vec[i].clear();
mpn.clear();
mpv.clear();
printf("Case #%d:\n", cas++);
int id=0;
while(gets(s1))
{
int len = strlen(s1);
if(s1[len-1]=='!') break;
memset(s, '\0', sizeof(s));
if(sscanf(s1, "%s%s%s%s%s%s", s[0],s[1],s[2],s[3],s[4],s[5])!=6)
{
if(s1[len-1]=='?')
{
s1[len-1]='\0';
sscanf(s1, "%s%s%s", s[0], s[1], s[2]);
a = s[1], b = s[2];
memset(vis, 0, sizeof(vis));
bool f;
if(mpn.find(a)==mpn.end()) mpn[a] = id++;
if(strcmp(s[0], "can")==0)
{
if(mpv.find(b)==mpv.end()) mpv[b] = id++;
f = dfs(mpn[a], mpv[b]);
}
else
{
if(mpn.find(b)==mpn.end()) mpn[b] = id++;
f = dfs(mpn[a], mpn[b]);
}
if(f) printf("Y");
else printf("M");
}
else
{
s1[len-1]='\0';
sscanf(s1, "%s%s%s", s[0], s[1], s[2]);
a=s[0], b=s[2];
if(mpn.find(a)==mpn.end()) mpn[a] = id++;
if(strcmp(s[1], "can")==0)
{
if(mpv.find(b)==mpv.end()) mpv[b] = id++;
vec[mpn[a]].push_back(mpv[b]);
}
else
{
if(mpn.find(b)==mpn.end()) mpn[b] = id++;
vec[mpn[a]].push_back(mpn[b]);
}
}
}
else
{
if(s1[len-1]=='?')
{
s1[len-1]='\0';
sscanf(s1,"%s%s%s%s%s%s",s[0],s[1],s[2],s[3],s[4],s[5]);
memset(vis, 0, sizeof(vis));
bool f;
a=s[4], b=s[5];
if(mpv.find(a)==mpv.end()) mpv[a] = id++;
if(strcmp(s[0], "can")==0)
{
if(mpv.find(b)==mpv.end()) mpv[b] = id++;
f = dfs(mpv[a], mpv[b]);
}
else
{
if(mpn.find(b)==mpn.end()) mpn[b] = id++;
f = dfs(mpv[a], mpn[b]);
}
if(f) printf("Y");
else printf("M");
}
else
{
s1[len-1]='\0';
sscanf(s1,"%s%s%s%s%s%s",s[0],s[1],s[2],s[3],s[4],s[5]);
a=s[3], b=s[5];
if(mpv.find(a)==mpv.end()) mpv[a] = id++;
if(strcmp(s[4],"can")==0)
{
if(mpv.find(b)==mpv.end()) mpv[b] = id++;
vec[mpv[a]].push_back(mpv[b]);
}
else
{
if(mpn.find(b)==mpn.end()) mpn[b] = id++;
vec[mpv[a]].push_back(mpn[b]);
}
}
}
}
putchar(10);
}
return 0;
}
(2) 用getchar输入 ,边读入,边保存到数组中,遇到空格就保存下一个单词
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <vector>
using namespace std;
map<string, int>mpn,mpv;
vector<int>vec[300];
int t;
char str[20][20], len[20];
int vis[300];
string a, b;
bool dfs(int x, int y)
{
vis[x]=1;
if(x==y) return true;
for(int i=0; i<vec[x].size(); i++)
{
int v = vec[x][i];
if(!vis[v] && dfs(v, y)) return true;
}
return false;
}
int main()
{
scanf("%d", &t);
int cas=1;
while(t--)
{
mpn.clear();
mpv.clear();
memset(str, '\0', sizeof(str));
memset(len, 0, sizeof(len));
for(int i=0; i<=200; i++) vec[i].clear();
int c, li=0;
int n=0, v=100;
printf("Case #%d:\n", cas++);
getchar();
while((c = getchar()) != EOF)
{
if(c=='.')
{
if(li==2)
{
a=str[0], b=str[2];
if(strcmp(str[1], "can")==0)
{
if(mpn.find(a)==mpn.end()) mpn[a]=n++;
if(mpv.find(b)==mpv.end()) mpv[b]=v++;
vec[mpn[a]].push_back(mpv[b]);
}
else
{
if(mpn.find(a)==mpn.end()) mpn[a]=n++;
if(mpn.find(b)==mpn.end()) mpn[b]=n++;
vec[mpn[a]].push_back(mpn[b]);
}
}
else
{
a=str[3], b=str[5];
if(strcmp(str[4], "can")==0)
{
if(mpv.find(a)==mpv.end()) mpv[a]=v++;
if(mpv.find(b)==mpv.end()) mpv[b]=v++;
vec[mpv[a]].push_back(mpv[b]);
}
else
{
if(mpv.find(a)==mpv.end()) mpv[a]=v++;
if(mpn.find(b)==mpn.end()) mpn[b]=n++;
vec[mpv[a]].push_back(mpn[b]);
}
}
memset(str, '\0', sizeof(str));
memset(len, 0, sizeof(len));
li=0;
getchar();
}
else if(c=='?')
{
int h=0;
bool flag=false;
memset(vis, 0, sizeof(vis));
if(li==2)
{
a=str[1], b=str[2];
if(strcmp(str[0], "can")==0)
{
if(mpn.find(a)==mpn.end()) h=1;
if(mpv.find(b)==mpv.end()) h=1;
if(!h) flag = dfs(mpn[a],mpv[b]);
}
else
{
if(a==b) h=0, flag=true;
else
{
if(mpn.find(a)==mpn.end()) h=1;
if(mpn.find(b)==mpn.end()) h=1;
if(!h) flag = dfs(mpn[a],mpn[b]);
}
}
}
else
{
a=str[4], b=str[5];
if(strcmp(str[0],"can")==0)
{
if(a==b) h=0, flag=true;//这里一开始忘记特判了,所以一直wa
else
{
if(mpv.find(a)==mpv.end()) h=1;
if(mpv.find(b)==mpv.end()) h=1;
if(!h) flag = dfs(mpv[a],mpv[b]);
}
}
else
{
if(mpv.find(a)==mpv.end()) h=1;
if(mpn.find(b)==mpn.end()) h=1;
if(!h) flag=dfs(mpv[a],mpn[b]);
}
}
if(h==0&&flag) printf("Y");
else printf("M");
memset(str, '\0', sizeof(str));
memset(len, 0, sizeof(len));
li=0;
getchar();
}
else if(c=='!')
{
break;
}
else if(c==' ')
{
li++;
}
else
{
str[li][len[li]++] = c;
}
}
putchar(10);
}
return 0;
}