原题地址: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. mybatis的mapper.xml使用parameterType使用的报错

    错误在于一个写的get(Long id)的查询方法, 而在Mapper.xml中我定义了这个接收的参数的类型是int类型, 结果就报了如下的错误 org.mybatis.spring.MyBatisS ...

  2. 基础概念——理解IP地址和域名

    从程序员角度,可以把因特网看做是世界范围内的主机集合: 1)主机集合被映射为一组32位的IP地址. 2)这个IP地址被映射为一组称为因特网域名的标识符. 3)因特网主机上的进程能够通过连接和任何其他因 ...

  3. Django中的Session--实现登录

    Django中的Session--实现登录 Django Session  Session Session 是什么 Session保存在服务端的键值对. 为什么要有 Session Cookie 虽然 ...

  4. 求幂大法,矩阵快速幂,快速幂模板题--hdu4549

    hdu-4549 求幂大法.矩阵快速幂.快速幂 题目 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 ...

  5. OpenERP 中的on_change方法总结

    1.xml中应为on_change=""的形式 2.py文件中 self,cr,uid,ids为必备参数,后面的参数根据xml文件中的参数的数量而定 3.return的是一个字典, ...

  6. 3Q大战现高潮,360 推出Android "3Q" IM即时通讯,岁末年初3Q大战惊现高潮

    岁末年初3Q大战惊现高潮,360震撼推出Android "3Q" IM即时通讯       看过了QQ和360斗争的开端高潮,当然现在还不能说这场斗争已经结束,在我看来这次的事件未 ...

  7. thinkPHP5配置nginx环境无法打开(require(): open_basedir restriction in effect. File(/mnt/hgfs/root/tp5/thinkphp/start.php) is not within the allowed path(s)

    今天想把玩一下tp5,结果怎么都无法访问,每次都是报500错误,我把错误提示都打开看到下面的错误 require(): open_basedir restriction in effect. File ...

  8. BUG~JS

    2017-11-06 1.没想到啊,这么久了,居然会有这种错误.canvas绘制图片,图片路径出错.drawImage() 解决方法:测试各参数,不单单是要打印出来,还要注意打印的参数是否为有效值

  9. Go语言学习笔记四: 运算符

    Go语言学习笔记四: 运算符 这章知识好无聊呀,本来想跨过去,但没准有初学者要学,还是写写吧. 运算符种类 与你预期的一样,Go的特点就是啥都有,爱用哪个用哪个,所以市面上的运算符基本都有. 算术运算 ...

  10. KNN理解

    基本思想 K近邻算法,即是给定一个训练数据集,对新的输入实例,在训练数据集中找到与该实例最邻近的K个实例,这K个实例的多数属于某个类,就把该输入实例分类到这个类中.如下面的图: 通俗一点来说,就是找最 ...