博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java学习笔记(5)----使用正则表达式解决Google Code Jam Qualification2009赛题 Alien Language...
阅读量:5740 次
发布时间:2019-06-18

本文共 3271 字,大约阅读时间需要 10 分钟。

  原题地址:  

题目描述:

ProblemAfter 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.InputThe 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.OutputFor each test case, outputCase #X: Kwhere X is the test case number, starting from 1, and K indicates how many words in the alien language match the pattern.LimitsSmall dataset1 ≤ L ≤ 101 ≤ D ≤ 251 ≤ N ≤ 10Large dataset1 ≤ L ≤ 151 ≤ D ≤ 50001 ≤ N ≤ 500SampleInput      Output  3 5 4abcbcadacdbccba(ab)(bc)(ca)abc(abc)(abc)(abc)(zyx)bcCase #1: 2Case #2: 1Case #3: 3Case #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

 

转载于:https://www.cnblogs.com/dongling/p/5790851.html

你可能感兴趣的文章
javascript之DOM基本操作(w3c标准)
查看>>
用JDBC查询学生成绩单
查看>>
SpringCloud拦截器使用(Interceptors拦截器使用)
查看>>
基于OSSIM平台的信息系统安全风险评估实施指南
查看>>
java匿名类的使用
查看>>
CentOS7安装Jenkins(稳定版)
查看>>
JDBC--使用DatabaseMetaData获取数据库信息
查看>>
makefile(三)使用命令
查看>>
AC AP
查看>>
[日推荐] 『忆年共享相册』-照片鉴证一起走过的哪些年
查看>>
Tomcat Session 共享 方法
查看>>
从零开始写一个武侠冒险游戏-6-用GPU提升性能(3)
查看>>
2015年工作总结
查看>>
在64位win7上安装visual studio 6.0
查看>>
Smart 2.0 开发指南
查看>>
»Spring 之AOP AspectJ切入点语法详解(最全了,不需要再去其他地找了)
查看>>
有感于DELL官网的修正速度和服务态度
查看>>
金三银四铜五铁六,面试得做好这个准备
查看>>
算法之【插入排序法】
查看>>
为UILabel 添加圆角背景(转)
查看>>