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 ...
随机推荐
- Tomcat 基础优化
作者:北京运维 本文档是身边一些朋友.技术大佬之前分享的一些笔记,记录了 Tomcat 优化方法,笔记较多而且比较杂乱,经过整理.分类我个人觉得大致可以从以下几个方面优化 Tomcat: Tomcat ...
- eclipse如何设置UTF-8
一.Eclipse设置utf-8编码包括两个方面,一方面可以设置workspace工作间编码,另一方面可以设置Android Project项目编码,设置步骤: 1)设置workspace工作间编码: ...
- 题解 [ZJOI2010]数字计数
传送门<-洛谷版 电梯<-bzoj版 这份代码是新手友好版,也算是自用版,注释自认为写的很详细. 希望对要学数位dp的人有所帮助 这份题解是记忆化搜索版的数位DP,个人还是比较建议用这种方 ...
- HDU - 6096 处理后缀的字典树
题意:给定n个字符串,m次询问,每次询问多少个字符串前缀是pre且后缀是suf,前后缀不可相交 字典树同时存储前后缀,假设字符串长为len则更新2*len个节点,依次按s[0],s[len-1],s[ ...
- Pycharm 报错 AttributeError: module 'pip' has no attribute 'main'
1.打开文件packaging_tool.py: D:\Program files\pycharm\PyCharm 2016.3.2\helpers\packaging_tool.py 2.添加导入: ...
- kafka多线程消费topic的问题
案例: topic:my-topic,分区:6 消费者:部署三台机器,每台机器上面开启6个线程消费. 消费结果:只有一台机器可以正常消费,另外两台机器直接输出六条告警日志: No broker par ...
- nginx设置开机自启动
每次启动nginx服务都需要到安装目录下的/sbin下面,感觉挺麻烦的. 下面介绍一下如何在Linux(CentOS)系统上,设置nginx开机自启动. 1 用脚本管理nginx服务 第一步:在/et ...
- centos6.5下安装zip格式的tomcat7和tomcat8,并同时运行
前提: 我的tomcat7安装在 /home/hadoop/software/apache-tomcat-7.0.69, tomcat8安装在 /home/hadoop/software/apache ...
- leetcode简单题目两道(5)
Problem Given an integer (signed bits), write a function to check whether it . Example: Given num = ...
- JAVA泛型——基本使用
Java1.5版本推出了泛型,虽然这层语法糖给开发人员带来了代码复用性方面的提升,但是这不过是编译器所做的一层语法糖,在真正生成的字节码中,这类信息却被擦除了.笔者发现很多几年开发经验的程序员,依然不 ...