京东2019年春暑期实习笔试

1.

 实验室大佬AC代码

 1 package testJD;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Scanner;
 5 
 6 public class test2 {
 7     public static void main(String[] args) {
 8         Scanner sc = new Scanner(System.in);
 9         int num = sc.nextInt();
10         sc.nextLine();
11         ArrayList<String> input = new ArrayList<>();
12         for(int i=0;i<num;i++){
13             input.add(sc.nextLine());
14         }
15         String target = sc.nextLine();
16         int count = 0;
17         for(int i=0;i<input.size();i++){
18             int[] next = kmpnext(input.get(i));
19             while(kmp(target,input.get(i),next)!=-1){
20                 int res = kmp(target, input.get(i),next);
21                 target=target.substring(0,res)+target.substring(res+input.get(i).length());
22 //                System.out.println("target: "+target);
23                 count++;
24             }
25         }
26         System.out.println(count);
27     }
28 
29     public static int kmp(String str, String dest,int[] next){//str文本串  dest 模式串
30         for(int i = 0, j = 0; i < str.length(); i++){
31             while(j > 0 && str.charAt(i) != dest.charAt(j)){
32                 j = next[j - 1];
33             }
34             if(str.charAt(i) == dest.charAt(j)){
35                 j++;
36             }
37             if(j == dest.length()){
38                 return i-j+1;
39             }
40         }
41         return -1;
42     }
43     public static int[] kmpnext(String dest){
44         int[] next = new int[dest.length()];
45         next[0] = 0;
46         for(int i = 1,j = 0; i < dest.length(); i++){
47             while(j > 0 && dest.charAt(j) != dest.charAt(i)){
48                 j = next[j - 1];
49             }
50             if(dest.charAt(i) == dest.charAt(j)){
51                 j++;
52             }
53             next[i] = j;
54         }
55         return next;
56     }
57 }

 2.

 

 过64%的代码

 1 package testJD;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Scanner;
 5 
 6 public class test2 {
 7     public static void main(String[] args) {
 8         Scanner sc = new Scanner(System.in);
 9         int num = sc.nextInt();
10         sc.nextLine();
11         ArrayList<String> input = new ArrayList<>();
12         for(int i=0;i<num;i++){
13             input.add(sc.nextLine());
14         }
15         String target = sc.nextLine();
16         int count = 0;
17         for(int i=0;i<input.size();i++){
18             int[] next = kmpnext(input.get(i));
19             while(kmp(target,input.get(i),next)!=-1){
20                 int res = kmp(target, input.get(i),next);
21                 target=target.substring(0,res)+target.substring(res+input.get(i).length());
22 //                System.out.println("target: "+target);
23                 count++;
24             }
25         }
26         System.out.println(count);
27     }
28 
29     public static int kmp(String str, String dest,int[] next){//str文本串  dest 模式串
30         for(int i = 0, j = 0; i < str.length(); i++){
31             while(j > 0 && str.charAt(i) != dest.charAt(j)){
32                 j = next[j - 1];
33             }
34             if(str.charAt(i) == dest.charAt(j)){
35                 j++;
36             }
37             if(j == dest.length()){
38                 return i-j+1;
39             }
40         }
41         return -1;
42     }
43     public static int[] kmpnext(String dest){
44         int[] next = new int[dest.length()];
45         next[0] = 0;
46         for(int i = 1,j = 0; i < dest.length(); i++){
47             while(j > 0 && dest.charAt(j) != dest.charAt(i)){
48                 j = next[j - 1];
49             }
50             if(dest.charAt(i) == dest.charAt(j)){
51                 j++;
52             }
53             next[i] = j;
54         }
55         return next;
56     }
57 }

猜你喜欢

转载自www.cnblogs.com/greatLong/p/10703277.html