Java学习笔记(5)----使用正则表达式解决Google Code Jam Qualification2009赛题 Alien Language
原题地址: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的更多相关文章
- java学习笔记16(正则表达式)
正则表达式: 定义:在pattern类中有简单规则定义,具有特殊含义的字符串: 作用:用于一些字符串,比如验证注册邮箱,密码,用户名等: 正则表达式的语法: 1)字符:'\'反斜杠 \t 代表制表 ...
- 《Java学习笔记(第8版)》学习指导
<Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...
- Java学习笔记4
Java学习笔记4 1. JDK.JRE和JVM分别是什么,区别是什么? 答: ①.JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库. ②.JRE(Java Run ...
- 20155234 2610-2017-2第九周《Java学习笔记》学习总结
20155234第九周<Java学习笔记>学习总结 教材学习内容总结 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC(Ja ...
- 20145330第十周《Java学习笔记》
20145330第十周<Java学习笔记> 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就 ...
- 20145330第九周《Java学习笔记》
20145330第九周<Java学习笔记> 第十六章 整合数据库 JDBC入门 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JD ...
- 20145330第八周《Java学习笔记》
20145330第八周<Java学习笔记> 第十五章 通用API 通用API 日志:日志对信息安全意义重大,审计.取证.入侵检验等都会用到日志信息 日志API Logger:注意无法使用构 ...
- 20145330第七周《Java学习笔记》
20145330第七周<Java学习笔记> 第十三章 时间与日期 认识时间与日期 时间的度量 GMT(格林威治标准时间):现在不是标准时间 世界时(UT):1972年UTC出来之前,UT等 ...
- 20145330第五周《Java学习笔记》
20145330第五周<Java学习笔记> 这一周又是紧张的一周. 语法与继承架构 Java中所有错误都会打包为对象可以尝试try.catch代表错误的对象后做一些处理. 使用try.ca ...
随机推荐
- 任务调度SpringTask
一.什么是任务调度 在企业级应用中,经常会制定一些“计划任务”,即在某个时间点做某件事情,核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作.常见的任务调度框架有Quartz和Spri ...
- linux下不同颜色文件的性质
绿色文件: 可执行文件,可执行的程序 红色文件:压缩文件或者包文件 蓝色文件:目录 白色文件:一般性文件,如文本文件,配置文件,源码文件等 浅蓝色文件:链接文件,主要是使用ln命令建立的文件 红色闪烁 ...
- 牛客Wannafly挑战赛26E 蚂蚁开会(树链剖分+线段树)
传送门 题面描述 一颗n个节点的树,m次操作,有点权(该节点蚂蚁个数)和边权(相邻节点的距离). 三种操作: 操作1:1 i x将节点i的点权修改为x.(1 <= i <= n; 1 &l ...
- [Xamarin.Android]如何引用JAR檔案 (转帖)
這個範例是如何在Xamarin.Android中去使用一個我們自行在開發的JAR檔案. 主要會執行的步驟如下 1. 在Xamarin建立一個Android Java Bindings Library ...
- 【es6】数值扩展
- 思维导图让 Spring MVC 不再难懂
spring mvc简介与运行原理 Spring的模型-视图-控制器(MVC)框架是围绕一个DispatcherServlet来设计的,这个Servlet会把请求分发给各个处理器,并支持可配置的处理器 ...
- 解决windows10下总是很快自动黑屏进入睡眠问题
在用win10的过程中总是过几分钟不操作电脑,就自动黑屏睡眠了. 下面讲解一下如何解决这个问题: 第一步:win +r 输入regedit.exe 运行注册表管理器 第二步:定位到 HKEY_LOC ...
- Maven项目中Spring整合Mybatis
Maven项目中Spring整合Mybatis 添加jar包依赖 spring需要的jar包依赖 <dependency> <groupId>org.springframewo ...
- ThreadPoolExecutor 异常
通过execute提交的任务,能将该任务抛出的异常交给未捕获异常处理器处理,而通过submit提交的任务,无论是抛出的未检查异常还是已检查异常,都将被认为是任务返回状态的一部分.如果一个由submit ...
- resotreIpAddress
Given a string containing only digits, restore it by returning all possible valid IP address combina ...