原题地址: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. elment 中 el-table 进行校验

    脑洞大开:什么是展示数据最好的方式呢,表格,写得又快,又清晰,又明显,那么就积累一些工作中表格经常使用到的东西. 第一步:效果图: 第二步:举个例子: <template> <div ...

  2. 网络基础 04_IP编址

    1 IP地址简介 什么是IP地址 在IP网络中,任何一个节点都需要一个唯一的IP IPV4 :32位 点分十进制 2 IP编址分类 有类编址 IP地址的类别 IP地址类型 网络地址:指代网络的地址.在 ...

  3. 【CF1157F】Maximum Balanced Circle 求一个相邻元素之间绝对值为小于1的最大环

    题目: https://codeforces.com/contest/1157/problem/F 给出一个序列 , 我们要从序列里面挑出一些数构造成一个相邻元素之间绝对值为小于1的最大环 , 挑选的 ...

  4. Bomb(要49)--数位dp

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submi ...

  5. Mac 10.12安装图片标注工具PxCook

    说明:现在基本是PxCook最好用,其余都是收费的,并且支持Android标注dp,主要是用于App开发时坐标定位,求到比例等等. 下载: (链接: https://pan.baidu.com/s/1 ...

  6. 5 个 iOS 和 Android 最佳的开源自动化工具[转]

    自动化测试时下在产品测试上有着非常重要的作用.实现测试自动化有多种积极的方式,包括最大限度地减少测试执行时间:在关键的发布阶段,用更少的时间确保更大的覆盖范围:在产品开发阶段,可靠又重复性地运行以确保 ...

  7. Google发布移动网站设计原则

    Google 刚刚发布了由 Google 与 AnswerLab 联合打造,名为<Principles of Mobile Site Design: Delight Users and Driv ...

  8. Redis-集群 - 分片

    Redis是一个基于内存的数据库,其不仅读写速度快,每秒可以执行大约110000的写操作,81000的读取操作,而且其支持存储字符串,哈希结构,链表,集合丰富的数据类型.所以得到很多开发者的青睐.加之 ...

  9. 基于Flume做FTP文件实时同步的windows服务。

    需求:做一个windows服务,实现从ftp服务器实时下载或者更新文件到本地磁盘. 功能挺简单的.直接写个ftp工具类用定时器跑就能搞定,那我为什么不用呢? 别问,问就是我无聊啊,然后研究一下Flum ...

  10. xcopy命令的其他参数

    xcopy /s /e /h "c:\123" "D:\123\" 后面多一个斜杠,让程序知道是目录 以下还给您提供了 xcopy 命令的其他参数: /A 仅复 ...