ZOJ-1038

41 篇文章 0 订阅
19 篇文章 0 订阅

高效的做法应该是用前缀树,这里我直接暴力枚举模拟了,也能过。。

import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.regex.Pattern;

public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		String[] ps = new String[] { "", "", "[abc]", "[def]", "[ghi]",
				"[jkl]", "[mno]", "[pqrs]", "[tuv]", "[wxyz]" };
		int N = sc.nextInt();
		Map<String, Integer> dict = new TreeMap<String, Integer>();
		String line;
		for (int t = 1; t <= N; t++)
		{
			System.out.format("Scenario #%d:\n", t);
			dict.clear();
			int w = sc.nextInt();
			sc.nextLine();
			while (w-- > 0)
			{
				line = sc.nextLine();
				String[] ss = line.split(" ");
				dict.put(ss[0], Integer.valueOf(ss[1]));
			}
			int m = sc.nextInt();
			sc.nextLine();
			while (m-- > 0)
			{
				line = sc.nextLine();
				String pattern = "";
				for (int i = 0; i < line.length() - 1; i++)
				{
					pattern += ps[line.charAt(i) - '0'];
					Pattern p = Pattern.compile(pattern);
					String curr, prev = null, res = null;
					int prob = 0, max = -1;
					for (String key : dict.keySet())
					{
						if (key.length() >= (i + 1)
								&& p.matcher(key.substring(0, i + 1)).find())
						{
							curr = key.substring(0, i + 1);
							if (curr.equals(prev))
								prob += dict.get(key);
							else
								prob = dict.get(key);
							if (prob > max)
							{
								max = prob;
								res = curr;
							}
							prev = curr;
						}
					}
					if (res != null)
						System.out.println(res);
					else
						System.out.println("MANUALLY");
				}
				System.out.println();
			}
			System.out.println();
		}
		sc.close();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值