ccf考试201809_3元素选择器
代码评分:40
总结:(1)还是那个意思,如果在较短的时间内拿到差不多的分数就够了,其他时间可以放在第4题上面试试能不能也拿几十分。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//1.初始化数据
Scanner scanner = new Scanner(System.in);
int contextLinesNum = scanner.nextInt();
int searchNum = scanner.nextInt();
//每一个元素(前面的空格数,标签,id,第几行)
String contexts[][] = new String[contextLinesNum][4];
for (int i = 0; i < contexts.length; i++) {
Arrays.fill(contexts[i],"null");
contexts[i][contexts[i].length - 1] = (i+1)+"";
}
//String searchs[] = new String[searchNum];
scanner.nextLine(); //将尾部的数据清除掉
//输入
for (int i = 0; i <contexts.length ; i++) {
String context = scanner.nextLine();
String line[] = context.split("(\\s|\\.)");
int blank = 0;
for (String word : line){
if(word.equals("")){
blank++;
}else if(!word.startsWith("#") && word!=""){
contexts[i][1] = word.toLowerCase();
}else if(word.startsWith("#")){
contexts[i][2] = word;
}
}
contexts[i][0] = blank+"";
}
//开始匹配
ArrayList<Integer> resultLine = null;
for (int i = 0; i < searchNum; i++) {
String search = scanner.nextLine();
//System.out.println(search);
resultLine = new ArrayList<Integer>();
//遍历context,查找需要的元素位置
if(search.startsWith("#")){
for (int j = 0; j < contexts.length; j++) {
if(contexts[j][2].equals(search)){
resultLine.add(Integer.parseInt(contexts[j][3]));
}
}
}else {
for (int j = 0; j < contexts.length; j++) {
if(contexts[j][1].equals(search)){
resultLine.add(Integer.parseInt(contexts[j][3]));
}
}
}
System.out.print(resultLine.size() +" ");
for (int line: resultLine){
System.out.print(line + " ");
}
System.out.println();
}
//getArr2(contexts);
//System.out.println("===================");
//getArr(searchs);
}
//打印arr
public static void getArr(String arr[]){
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
//打印二维数组
public static void getArr2(String arr[][]){
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}