码迷,mamicode.com
首页 > 编程语言 > 详细

Java学习笔记(5)----使用正则表达式解决Google Code Jam Qualification2009赛题 Alien Language

时间:2016-08-20 19:22:21      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

  原题地址: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

标签:

原文地址:http://www.cnblogs.com/dongling/p/5790851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!