Excuses, Excuses!

题目:Judge Ito is having a problem with people subpoenaed for jury duty giving rather lame excuses in order to avoid serving. In order to reduce the amount of time required listening to goofy excuses, Judge Ito has asked that you write a program that will search for a list of keywords in a list of excuses identifying lame excuses. Keywords can be matched in an excuse regardless of case.
思路:字符串的处理

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {

    public static void main(String[] args){
        Scanner input=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        while (input.hasNext()) {
            String s=input.nextLine();
            String[] sNum=s.split(" ");
            int k=Integer.parseInt(sNum[0]);
            int e=Integer.parseInt(sNum[1]);
            String[] strk=new String[k];
            String[] stre=new String[e];
            String[] strel=new String[e];
            int[] count=new int[e];
            int max=0;
            for (int i = 0; i < strk.length; i++) {
                strk[i]=input.nextLine().toLowerCase(); 
            }
            for (int i = 0; i < strel.length; i++) {
                stre[i]=input.nextLine();
                strel[i]=stre[i].toLowerCase();
                String temp="";
                for (int j = 0; j < strel[i].length(); j++) {
                    if (Character.isLetter(strel[i].charAt(j))) {//判断strel[i].charAt(j)是否为字母
                        temp=temp+strel[i].charAt(j);
                    }else {
                        temp=temp+"#";//若不是字母则用#代替
                    }
                }
                strel[i]=temp;
            //借口字符串和关键词字符串进行处理
                for (int j = 0; j < strk.length; j++) {
                    //分情况讨论  当strel[i]存在关键词时,进行计数
                    if (strel[i].indexOf("#"+strk[j]+"#")!=-1) {
                        count[i]++;
                    }else if (strel[i].indexOf(strk[j]+"#")==0) {
                        count[i]++;
                    }else if (strel[i].indexOf("#"+strk[j])+strk[j].length()==strel[i].length()) {
                        count[i]++;
                    }
                }   
            }
            //找出最大count值
            for (int i = 0; i < count.length; i++) {
                if (count[i]>max) {
                    max=count[i];
                }
            }
            //输出
            int index=1;
            System.out.println("Excuse Set #"+index++);
            for (int i = 0; i < count.length; i++) {
                if (count[i]==max) {
                    System.out.println(stre[i]);
                }
            }
            System.out.println();
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值