P1039 [NOIP2003 提高组] 侦探推理

注意事项:

  • Acwing 行末为 '\n',洛谷则 '\r\n',需要特殊处理。
  • 尽量多打一些调试代码,不要删掉。
  • 怎么处理字符串,放心大胆去做,个人建议不用自带函数处理(担心越界)
  • 时空开多大都行,数据很小
  • 枚举谁是罪犯,且这天是星期几。(因为这样可以判断真假)
  • 比如样例
3 1 5
MIKE
CHARLES
KATE
MIKE: I am guilty.
MIKE: Today is Sunday.
CHARLES: MIKE is guilty.
KATE: I am guilty.
KATE: How are you??

枚举到当 MIKE 是罪犯,今天是 'Sunday' 时候,则

第一句话是真的

第二句话是真的

第三句话是真的

第四句话是假的

第五句话没用

也就是说 MIKE 和 CHARLES 说真话的,KATE 说假话。
符合题意。

当前面这个人说的话和他现在说的不一样时候,一定是不合法的,直接退出。

最好复制题目的星期,因为你不知道你写不写对??

处理字符串时候一定要用 if else 语句,不然就会出现没用的化你算成有用的话,尤其是处理第三,四种操作时候。

别把输入的名字和后面输入话的名字搞混了。

下面贡献几组数据

2 2 4
HELLO
GUILTY
HELLO: What is your name?
GUILTY: I am GUILTY.
GUILTY: Are you guilty?
HELLO: I am not guilty.
HELLO
5 1 5
A
B
C
D
E
A: Today is Monday.
B: Today is Thursday.
C: Today is Monday.
B: D is not guilty.
E: I am not guilty.
D
10 7 20
A
AA
AAA
AAAA
AAAAA
AAAAAA
AAAAAAA
AAAAAAAA
AAAAAAAAA
AAAAAAAAAA
A: Today is Monday.
AA: Today is Monday.
AAA: Today is Monday.
AAAA: Today is Monday.
AAAAA: Today is Monday.
AAAAAA: Today is Monday.
AAAAAAA: Today is Monday.
AAAAAAAA: Today is Sunday.
AAAAAAAAA: Today is Sunday.
AAAAAAAAAA: Today is Sunday.
AAAAAAAAAA: AAA is not guilty.
AAAAAAAAA: A is not guilty.
AAAAAAAA: AAAAA is not guilty.
AAAAAAA: AAAAAA is guilty.
AAAAAA: AAAAAAAAAA is guilty.
AAAAA: AAAAAAAA is guilty.
AAAA: AAAAAAA is guilty.
AAA: AA is guilty.
AA: AAAAAAAAA is guilty.
A: AAAAA is guilty.
AAAA
3 3 3
SAM
SANDY
SUE
SAM: I am not guity. Am I???
SUE: SANDY is guity.
SANDY: SUE is guity.
Cannot Determine

代码

#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i <= r; ++ i)
#define per(i, r, l) for(int i = r; i >= l; -- i)
using namespace std;
const int N = 1000;
int n, m, p;
string name[N], sentence[N];
map<string, int>mp;
struct E{
	string name, name1;
	int id;
	int day;
}e[N];
int solve(int day, int T)
{
	mp.clear();
	rep(i, 1, p)
	{
		if(e[i].id == 1)
		{
			if(e[i].name == name[T])
			{
				if(mp[e[i].name] == 2) return 0;
				mp[e[i].name] = 1;
			}
			else 
			{
				if(mp[e[i].name] == 1) return 0;
				mp[e[i].name] = 2;
			}
		}
		else if(e[i].id == 2) 
		{
			if(e[i].name == name[T])
			{
				if(mp[e[i].name] == 1) return 0;
				mp[e[i].name] = 2;
			}
			else 
			{
				if(mp[e[i].name] == 2) return 0;
				mp[e[i].name] = 1;
			}
		}
		else if(e[i].id == 3)
		{
			if(e[i].name1 == name[T]) 
			{
				if(mp[e[i].name] == 2) return 0;
				mp[e[i].name] = 1;
			}
			else
			{
				if(mp[e[i].name] == 1) return 0;
				mp[e[i].name] = 2; 
			}
		}
		else if(e[i].id == 4)
		{
			if(e[i].name1 == name[T]) 
			{
				if(mp[e[i].name] == 1) return 0;
				mp[e[i].name] = 2;
			}
			else
			{
				if(mp[e[i].name] == 2) return 0;
				mp[e[i].name] = 1; 
			}
		}
		else if(e[i].id == 5)
		{
			if(day == e[i].day)
			{
				if(mp[e[i].name] == 2) return 0;
				mp[e[i].name] = 1;
			}
			else
			{
				if(mp[e[i].name] == 1) return 0;
				mp[e[i].name] = 2; 
			}
		}
	}
	int tot = 0, tot1 = 0, tot2 = 0;
	rep(i, 1, m)
	{
		if(mp[name[i]] == 1) ++ tot;
		else if(mp[name[i]] == 2) ++ tot1;
		else ++ tot2;
	}
	if(tot1 > n) return 1;
	if(tot1 + tot2 >= n) return 2;
	return 0;
}
main()
{
	cin >> m >> n >> p; 
	string sssss;
	getline(cin,sssss);
    rep(i, 1, m)  
    {
		getline(cin, sssss);
		rep(j, 0, (int)sssss.size() - 1) if(sssss[j] != '\n' && sssss[j] != '\r') name[i] += sssss[j];
    }
    rep(i, 1, p) 
    {
		getline(cin, sssss);
		rep(j, 0, (int)sssss.size() - 1) if(sssss[j] != '\n' && sssss[j] != '\r') sentence[i] += sssss[j];
    	rep(j, 1, m) 
    	{
    		string s = "";
    		for(int k = 0; sentence[i][k] != ':'; ++ k) s += sentence[i][k];
    		if(name[j] == s) 
    		{
    			e[i].name = name[j];
    			string ss = "";
    			rep(k, (int)name[j].size() + 2, (int)sentence[i].size() - 1) ss += sentence[i][k];
    			if(ss == "I am guilty.") e[i].id = 1;
    			else if(ss == "I am not guilty.") e[i].id = 2;
    			else
    			{
    				string sss = "";
    				rep(k, 0, 7) sss += ss[k];
    				if(sss == "Today is")
    				{
    					e[i].id = 5;
    					string ssss = "";
    					rep(k, 9, (int)ss.size() - 2) ssss += ss[k];
    					if(ssss == "Monday") e[i].day = 1;
    					if(ssss == "Tuesday") e[i].day = 2;
    					if(ssss == "Wednesday") e[i].day = 3;
    					if(ssss == "Thursday") e[i].day = 4;
    					if(ssss == "Friday") e[i].day = 5;
    					if(ssss == "Saturday") e[i].day = 6;
    					if(ssss == "Sunday") e[i].day = 7;
    				}
    				else
    				{
	    				rep(k, 1, m)
	    				{
	    					string sss = "";
	    					for(int l = 0; ss[l] != ' '; ++ l) sss += ss[l];
	    					if(sss == name[k])
	    					{
	    						string ssss = "";
	    						rep(h, (int)name[k].size() + 1, (int)ss.size() - 2) ssss += ss[h];
	 							if(ssss == "is guilty") e[i].id = 3, e[i].name1 = sss;
	 							else if(ssss == "is not guilty") e[i].id = 4, e[i].name1 = sss;
	 							break;
	    					}
	    				}
    				}
    			}
    			break;
    		}
    	}
    }
    string ss = "";
    rep(i, 1, 7) rep(j, 1, m) 
	{
		int x = solve(i, j);
		if(x == 2) 
		{
			if(ss != "" && ss != name[j]) cout << "Cannot Determine", exit(0);
			ss = name[j]; 
		}
	}
	cout << (ss == "" ? "Impossible" : ss);
}
posted @ 2024-11-02 09:33  liukejie  阅读(7)  评论(0编辑  收藏  举报