编程之美热身赛 题目1 : 传话游戏

时间限制: 2000ms
单点时限: 1000ms
内存限制: 256MB

描述

Alice和Bob还有其他几位好朋友在一起玩传话游戏。这个游戏是这样进行的:首先,所有游戏者按顺序站成一排,Alice站第一位,Bob站最后一位。然后,Alice想一句话悄悄告诉第二位游戏者,第二位游戏者又悄悄地告诉第三位,第三位又告诉第四位……以此类推,直到倒数第二位告诉Bob。两位游戏者在传话中,不能让其他人听到,也不能使用肢体动作来解释。最后,Bob把他所听到的话告诉大家,Alice也把她原本所想的话告诉大家。

由于传话过程中可能出现一些偏差,游戏者越多,Bob最后听到的话就与Alice所想的越不同。Bob听到的话往往会变成一些很搞笑的东西,所以大家玩得乐此不疲。经过几轮游戏后,Alice注意到在两人传话中,有些词汇往往会错误地变成其他特定的词汇。Alice已经收集到了这样的一个词汇转化的列表,她想知道她的话传到Bob时会变成什么样子,请你写个程序来帮助她。


输入

输入包括多组数据。第一行是整数 T,表示有多少组测试数据。每组数据第一行包括两个整数 N 和 M,分别表示游戏者的数量和单词转化列表长度。随后有 M 行,每行包含两个用空格隔开的单词 a 和 b,表示单词 a 在传话中一定会变成 b。输入数据保证没有重复的 a。最后一行包含若干个用单个空格隔开的单词,表示Alice所想的句子,句子总长不超过100个字符。所有单词都只包含小写字母,并且长度不超过20,同一个单词的不同时态被认为是不同的单词。你可以假定不在列表中的单词永远不会变化。

1 ≤ T ≤ 100
小数据:2 ≤ N ≤ 10, 0 ≤ M ≤ 10 
大数据:2 ≤ N ≤ 100, 0 ≤ M ≤ 100

输出

对于每组测试数据,单独输出一行“Case #c: s”。其中,c 为测试数据编号,s 为Bob所听到的句子。s 的格式与输入数据中Alice所想的句子格式相同。

样例输入
2
4 3
ship sheep
sinking thinking
thinking sinking
the ship is sinking
10 5
tidy tiny
tiger liar
tired tire
tire bear
liar bear
a tidy tiger is tired
样例输出
Case #1: the sheep is thinking
Case #2: a tiny bear is bear
package TransSentence;

import java.io.BufferedInputStream;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
	public int pNum;
	public int condition;
	public String strs[];

	public Main() {
	}

	public Main(int pNum, int condition, String[] strs) {
		this.pNum = pNum;
		this.condition = condition;
		this.strs = strs;
	}

	public void process() {
		HashMap<String, String> hm = new HashMap<String, String>(condition);
		for (int i = 0; i < condition; i++) {
			hm.put(strs[i].split(" ")[0], strs[i].split(" ")[1]);
		}
		String[] sen = strs[condition].split(" ");
		for (int j = 0; j < sen.length; j++) {
			for (int i = 1; i < pNum; i++) {
				if (hm.containsKey(sen[j])){
//					System.out.println(sen[j]);
					sen[j] = hm.get(sen[j]);
//					System.out.println(sen[j]);
				}
			}
		}
		for(int i=0;i<sen.length;i++)
			System.out.print(sen[i]+" ");
		System.out.println("");
		
	}

/*	@Override
	public String toString() {
		System.out.println("人数:" + this.pNum + " 单词转化列表长度" + this.condition
				+ " 话:" + strs[strs.length - 1]);
		return super.toString();
	}*/

	public static void main(String[] args) {
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		int num = cin.nextInt();
		cin.nextLine();

		for (int i = 0; i < num; i++) {
			int pNum = cin.nextInt();
			int condition = cin.nextInt();
			cin.nextLine();
			String[] strs = new String[condition + 1];
			for (int j = 0; j < condition + 1; j++) {
				String s = cin.nextLine();
//				System.out.println(s);
				strs[j] = s;
			}

			Main ts = new Main(pNum, condition, strs);
//			ts.toString();
			System.out.print("Case #"+ (i+1) +": ");
			ts.process();
		}
		cin.close();
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值