题目: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();
}
}
}
本文介绍了一个用于识别人们为逃避陪审团职责而提出的各种借口的程序。该程序通过匹配关键词来快速筛选出那些试图逃避责任的借口,从而提高法官的工作效率。
267

被折叠的 条评论
为什么被折叠?



