原题地址:https://code.google.com/codejam/contest/90101/dashboard#s=p0  

题目描述:

Problem

After years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L lowercase letters. Also, there are exactly D words in this language.

Once the dictionary of all the words in the alien language was built, the next breakthrough was to discover that the aliens have been transmitting messages to Earth for the past decade. Unfortunately, these signals are weakened due to the distance between our two planets and some of the words may be misinterpreted. In order to help them decipher these messages, the scientists have asked you to devise an algorithm that will determine the number of possible interpretations for a given pattern.

A pattern consists of exactly L tokens. Each token is either a single lowercase letter (the scientists are very sure that this is the letter) or a group of unique lowercase letters surrounded by parenthesis ( and ). For example: (ab)d(dc) means the first letter is either a or b, the second letter is definitely d and the last letter is either d or c. Therefore, the pattern (ab)d(dc) can stand for either one of these 4 possibilities: add, adc, bdd, bdc.

Input

The first line of input contains 3 integers, L, D and N separated by a space. D lines follow, each containing one word of length L. These are the words that are known to exist in the alien language. N test cases then follow, each on its own line and each consisting of a pattern as described above. You may assume that all known words provided are unique.

Output

For each test case, output

Case #X: K
where X is the test case number, starting from 1, and K indicates how many words in the alien language match the pattern. Limits Small dataset 1 ≤ L ≤ 10
1 ≤ D ≤ 25
1 ≤ N ≤ 10
Large dataset 1 ≤ L ≤ 15
1 ≤ D ≤ 5000
1 ≤ N ≤ 500
Sample Input Output 3 5 4
abc
bca
dac
dbc
cba
(ab)(bc)(ca)
abc
(abc)(abc)(abc)
(zyx)bc
Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0

算法代码:

package code;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class Solution { public static void main(String args[]) throws Exception{
InputStream in=new FileInputStream(new File("GoogleCodeJam/A-large-practice.in"));
File out=new File("GoogleCodeJam/A-large-practice.out");
output(in, out);
System.out.println("可以查看结果了");
in.close(); } /**
* 办算法充分利用Java的正则表达式来进行字符串的匹配,算法由此变得简单
* @param in 数据输入流
* @param out 保存结果的数据输出流
* @throws FileNotFoundException
*/
public static void output(InputStream in,File out) throws FileNotFoundException{
int L,D,N;
Scanner reader=new Scanner(in);
L=reader.nextInt();
D=reader.nextInt();
N=reader.nextInt();
//以下三行打印在控制台,用以提示用户程序运行正确与否
System.out.println("L="+L);
System.out.println("D="+D);
System.out.println("N="+N);
String [] pool=new String[D];
reader.nextLine(); //L,D,N 都是在同一行的,读完N后,剩下的一个换行符还在输入缓冲里,要用 nextLine() 函数将其去除掉
for(int i=0;i<D;i++){
pool[i]=reader.nextLine(); //读取dictionary中的所有的字符串
} PrintWriter writer=new PrintWriter(out);
int matchCount=0;
Pattern pLeft=Pattern.compile("\\(");
Pattern pRight=Pattern.compile("\\)");
for(int i=1;i<=N;i++){
String patStr=reader.nextLine(); //读取一个密文,下面的处理将其处理成Pattern
Matcher mLeft=pLeft.matcher(patStr);
String temp=mLeft.replaceAll("\\[");//将左括号换做左方括号
Matcher mRight=pRight.matcher(temp);
String temp2=mRight.replaceAll("\\]"); //将右括号换做右方括号,此时temp2就是期望的Pattern字符串
Pattern pattern=Pattern.compile(temp2);
matchCount=0;
for(int j=0;j<D;j++){
Matcher matcher=pattern.matcher(pool[j]);
if(matcher.find()){
if(matcher.start()==0&&matcher.end()==(pool[j].length())){
matchCount++;
}
}
}
System.out.println("Case #"+i+": "+matchCount);//显示在控制台,提示程序正在运行
writer.println("Case #"+i+": "+matchCount);
}
writer.close();
}
}

Java学习笔记(5)----使用正则表达式解决Google Code Jam Qualification2009赛题 Alien Language的更多相关文章

  1. java学习笔记16(正则表达式)

    正则表达式: 定义:在pattern类中有简单规则定义,具有特殊含义的字符串: 作用:用于一些字符串,比如验证注册邮箱,密码,用户名等: 正则表达式的语法: 1)字符:'\'反斜杠   \t 代表制表 ...

  2. 《Java学习笔记(第8版)》学习指导

    <Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...

  3. Java学习笔记4

    Java学习笔记4 1. JDK.JRE和JVM分别是什么,区别是什么? 答: ①.JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库. ②.JRE(Java Run ...

  4. 20155234 2610-2017-2第九周《Java学习笔记》学习总结

    20155234第九周<Java学习笔记>学习总结 教材学习内容总结 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC(Ja ...

  5. 20145330第十周《Java学习笔记》

    20145330第十周<Java学习笔记> 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就 ...

  6. 20145330第九周《Java学习笔记》

    20145330第九周<Java学习笔记> 第十六章 整合数据库 JDBC入门 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JD ...

  7. 20145330第八周《Java学习笔记》

    20145330第八周<Java学习笔记> 第十五章 通用API 通用API 日志:日志对信息安全意义重大,审计.取证.入侵检验等都会用到日志信息 日志API Logger:注意无法使用构 ...

  8. 20145330第七周《Java学习笔记》

    20145330第七周<Java学习笔记> 第十三章 时间与日期 认识时间与日期 时间的度量 GMT(格林威治标准时间):现在不是标准时间 世界时(UT):1972年UTC出来之前,UT等 ...

  9. 20145330第五周《Java学习笔记》

    20145330第五周<Java学习笔记> 这一周又是紧张的一周. 语法与继承架构 Java中所有错误都会打包为对象可以尝试try.catch代表错误的对象后做一些处理. 使用try.ca ...

随机推荐

  1. js 移动端获取当前用户的经纬度

    一.HTML5 geolocation的属性 if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(onSuccess ...

  2. Mac下安装Iterm2终端工具

    一般Iterm2是结合oh-my-zsh一起使用,但是如果不喜欢zsh也可以单独使用.Iterm2有个亮点就是可以通过快捷键快速启动. 安装步骤: 1.下载: http://www.iterm2.co ...

  3. preg_match 与 preg_match_all

    案例一: <?php $str = 'abcdef123456'; preg_match('/[a-z1-9]+/', $str, $res); var_dump($res); preg_mat ...

  4. 如何为 Go 设计一个通用的日志包

    需求 一个通用的日志包,应该满足以下几个需求: 兼容 log.Logger,标准库大量使用了 log.Logger 作为其错误内容的输出通道,比如 net/http.Server.ErrorLog,所 ...

  5. tomcat监控工具probe

    probe官网:http://www.lambdaprobe.org/ 但是已经链接至github了:https://github.com/psi-probe/psi-probe 下载psi-prob ...

  6. 13 Timer和TimerTask 示例

    定时器是一个应用十分广泛的线程工具,可用于调度多个定时任务以后台线程的方式执行.在Java中,可以通过Timer和TimerTask类来实现定义调度的功能 1 Timerjava.lang.Objec ...

  7. APU (美国AMD公司研发的加速处理器)

    APU(Accelerated Processing Unit)中文名字叫加速处理器,是AMD“融聚未来”理念的产品,它第一次将中央处理器和独显核心做在一个晶片上,它同时具有高性能处理器和最新独立显卡 ...

  8. git 切换 远程仓库

    $ git remote rm origin $ git remote add origin '仓库地址.git' $ git branch --set-upstream-to=origin/mast ...

  9. [转] SQL函数说明大全

    from http://www.cnblogs.com/moss_tan_jun/archive/2010/08/23/1806861.html 一旦成功地从表中检索出数据,就需要进一步操纵这些数据, ...

  10. C# 之程序退出的方法

    1.this.Close();   只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: 2.Application.Exit();  强制所有消息中 ...