• 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();
        }
    }
  • 相关阅读:
    思蕊防静电地板
    一个老站长的22条军规
    百度天天快照知识宝典
    搜索引擎常用搜索技巧
    网站运营工作流程
    关于线程间通信
    VS2012 EF5 连接oracle11.2
    ArcSde for Oracle服务注册
    NHibernate composite-id联合主键配置
    NHibernate 的 ID 标识选择器
  • 原文地址:https://www.cnblogs.com/dongling/p/5790851.html
Copyright © 2020-2023  润新知