UVA 489-- Hangman Judge(暴力串处理)

 Hangman Judge 

In ``Hangman Judge,‘‘ you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follows:

  1. The contestant tries to solve to puzzle by guessing one letter at a time.
  2. Every time a guess is correct, all the characters in the word that match the guess will be ``turned over.‘‘ For example, if your guess is ``o‘‘ and the word is ``book‘‘, then both ``o‘‘s in the solution will be counted as ``solved.‘‘
  3. Every time a wrong guess is made, a stroke will be added to the drawing of a hangman, which needs 7 strokes to complete. Each unique wrong guess only counts against the contestant once.
       ______
       |  |
       |  O
       | /|\
       |  |
       | / \
     __|_
     |   |______
     |_________|
  4. If the drawing of the hangman is completed before the contestant has successfully guessed all the characters of the word, the contestant loses.
  5. If the contestant has guessed all the characters of the word before the drawing is complete, the contestant wins the game.
  6. If the contestant does not guess enough letters to either win or lose, the contestant chickens out.

Your task as the ``Hangman Judge‘‘ is to determine, for each game, whether the contestant wins, loses, or fails to finish a game.

Input

Your program will be given a series of inputs regarding the status of a game. All input will be in lower case. The first line of each section will contain a number to indicate which round of the game is being played; the next line will be the solution to the
puzzle; the last line is a sequence of the guesses made by the contestant. A round number of -1 would indicate the end of all games (and input).

Output

The output of your program is to indicate which round of the game the contestant is currently playing as well as the result of the game. There are three possible results:

You win.
You lose.
You chickened out.

Sample Input

1
cheese
chese
2
cheese
abcdefg
3
cheese
abcdefgij
-1

Sample Output

Round 1
You win.
Round 2
You chickened out.
Round 3
You lose.

又是一道串处理的题,感觉模拟题大多跟串有关 orz。

这道题是模拟一个猜字谜游戏,一共同拥有两个串 第一个串是字谜的答案一開始都是翻着的(就是在反面 看不到),第二个串你猜的答案。对于你的答案。从左往右遍历,假设这个字符在答案中出现过。那么将答案串中全部包括这个字母的卡片都翻开,假设猜错了(就是答案串中找不到这个字符) 罚时加一(罚时7以上就算输了),并且,假设当前字母猜错了,以后再猜这个不加罚时。(比方答案中没有a 你第一次猜a 罚时加一,以后再猜a 不加罚时)假设在罚时限制内全猜完了,就算赢,假设在罚时时间内没猜完(意思是串遍历到头还没猜完但罚时也不到7)就算逃跑。

依据游戏特点,能够对输入的两个串分别进行删除反复字母处理(这点我不知道有没有必要,只是我那挫代码貌似比人家跑的慢了好多好多。。

大概就是这的问题)。然后以下模拟游戏操作就能够了

#include<cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
char s[1010], g[1010], t1[1010], t2[1010];
int main()
{
	int round, i, j, flag;

	while (cin >> round && round != -1)
	{
		int p = 0, q = 0;
		cin >> s >> g;

		for (i = 0; i < strlen(s); i++)//去掉反复字母
		{
			flag = 1;

			for (j = 0; j < i; j++)
			{
				if (s[j] == s[i])
				{
					flag = 0;
					break;
				}
			}

			if (flag)
			{
				t1[p++] = s[i];
			}
		}

		for (i = 0; i < strlen(g); i++)//去掉反复字母
		{
			flag = 1;

			for (j = 0; j < i; j++)
			{
				if (g[j] == g[i])
				{
					flag = 0;
					break;
				}
			}

			if (flag)
			{
				t2[q++] = g[i];
			}
		}

		cout << "Round " << round << endl;
		int cnt = 0, sum = 0, ok = 1;

		for (i = 0; i < q; i++)
		{
			int f = 1;

			for (j = 0; j < p; j++)
			{
				if (t2[i] == t1[j])
				{
					f = 0;
					break;
				}
			}

			if (f)
			{
				cnt++;//统计罚时
			}
			else
			{
				sum++;//统计已猜对的字母的个数
			}

			if (sum == p && cnt < 7)
			{
				ok = 0;
				cout << "You win." << endl;
				break;
			}

			if (cnt >= 7)
			{
				ok = 0;
				cout << "You lose." << endl;
				break;
			}
		}

		if (ok)
		{
			cout << "You chickened out." << endl;
		}
	}

	return 0;
}
时间: 2024-10-07 06:44:55

UVA 489-- Hangman Judge(暴力串处理)的相关文章

UVa 489 Hangman Judge(字符串)

 Hangman Judge  In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follo

uva 489.Hangman Judge 解题报告

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=430 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 using namespace std; 6 7 con

UVA 489 Hangman Judge (字符匹配)

题意:给一个字符串A,只含小写字符数个.再给一个字符串B,含小写字符数个.规则如下: 1.字符串B从左至右逐个字符遍历,对于每个字符,如果该字符在A中存在,将A中所有该字符删掉,若不存在,则错误次数+1. 2.当错误次数达到7时,游戏结束,输了lose. 3.当串A中已经没有字符了,游戏结束,赢了win.(串B后面还没遍历到的也不用遍历了) 4.当错误次数没到达7,但是字符串A还有剩下的字符没消去,则chickened out. 错满7个就输,在满7个之前匹配完了就赢,在满7个之前没匹配完就ch

UVa 489 Hangman Judge

题目大意: 这是一个猜单词的游戏. 输入回合数(但回合数为-1的时候停止) 再输入一个待猜的单词B 最后输入你猜的单词组合A. 这个猜单词比较有意思的地方是他一个一个判断. 从你输入的A中,按照顺序一个一个字母来判断. 当B中有这个字母的时候,这个字母就被标记(所有在B中相同的都被标记). 当B中没有这个字母的时候,就统计一次错. 若 所以字母都被标记了则获胜, 错误达到7次则失败. 若 判断完错误还不足7次, 也没标记完,则视为放弃. ★一定要注意是一个一个判断 Sample Input 1

UVA 10010-- Where&#39;s Waldorf?--暴力串处理

 Where's Waldorf?  Given a m by n grid of letters, ( ), and a list of words, find the location in the grid at which the word can be found. A word matches a straight, uninterrupted line of letters in the grid. A word can match the letters in the grid

【UVA】12169-Disgruntled Judge(暴力or欧几里得)

可能由于后台数据的原因,这道题直接暴力枚举a,b进行判断也能过,不过跑的时间长,效率太差了. 14021006 12169 Disgruntled Judge Accepted C++ 0.876 2014-08-11 08:46:28 不说了,比较无脑. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #incl

uva 489

In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follows: The contesta

uva 467 - Synching Signals(暴力+数学)

题目连接:uva 467 - Synching Signals 题目大意:有n个红绿灯,给出红灯的时间t,那么该灯从0时刻开始就以2*t为周期绿黄红三灯交替,时间分别为t-5,5,t.问所这n个等从第一变为有一个灯不为绿灯开始,要多久才能变成所有的灯刚好都为绿灯.时间超过1小时输出unable to synch after one hour. 解题思路:一小时才3600秒,枚举秒数判断即可. #include <cstdio> #include <cstring> #include

uva 618 - Doing Windows(暴力+数学)

题目链接:uva 618 - Doing Windows 题目大意:给出电脑桌面的大小W和H,现在在桌面上有4个窗口,给出窗口的初始大小,问说能不能通过调整各个窗口的大小(长宽比例不能变)使得4个屏幕刚好占满整个屏幕,并且互相不覆盖. 解题思路:其实可以直接暴力出所有情况,不过细节比较多,而且要考虑所有的细节. 我的做法的是先将4个窗口缩小至最小的状态,然后枚举左下角的窗口, 有四种可能 蓝色部分为另外枚举的窗口,3,4种情况要分别保证说长.宽相等,然后S部分就是子问题. 所以用一个二进制数来表