华为2019研发工程师实习生笔试题

问题一:对于给定的字符串,求连续最长回文串的长度。【AC-100%】

思路:将字符串反转,和原字符串求最长连续公共子序列(这样得到的就是最长的连续回文串)的长度即可。

 1 import java.util.Scanner;
 2 
 3 public class Main1 {
 4     public static int solve(String s1) {
 5         StringBuffer sb = new StringBuffer(s1);
 6         sb.reverse();
 7         String s2 = sb.toString();
 8         int len = s1.length();
 9         int[][] dp = new int[len+1][len+1];
10         for(int i = 0; i <= len; i++)
11             dp[i][0] = 0;
12         int result = -1;
13         for(int i = 1; i <= len; i++) {
14             for(int j = 1; j <= len; j++) {
15                 if(s1.charAt(i-1) == s2.charAt(j-1)) {
16                     dp[i][j] = dp[i-1][j-1] + 1;
17                 } else {
18                     dp[i][j] = 0;
19                 }
20                 if(dp[i][j] > result)
21                     result = dp[i][j];
22             }
23         }
24         return result;
25     }
26     public static void main(String[] args) {
27         Scanner sc = new Scanner(System.in);
28         while(sc.hasNext()) {
29             String str = sc.nextLine();
30             System.out.println(solve(str));
31         }
32     }
33 }

 

问题二:输入一个URL地址,根据给定的不同URL地址的种类特点(有些 0000:0000 的情况可以简写为 :: ),输出这个地址属于哪一类。【AC-70%】

思路:按照给定的不同种类特点去处理就可以了,简写的情况没有考虑怎么处理,所以没能完全AC=。=

 1 import java.util.Scanner;
 2 
 3 public class Main2 {
 4     public static String solve(String str) {
 5         String[] x = str.split(":");
 6         int len = x.length;
 7         String result = "Error";
 8         if(str.equals("::")) {
 9             result = "GlobalUnicast";
10         }
11         if(str.equals("::1") || str.equals("::0001")) {
12             result = "Loopback";
13         }
14         
15         if(x.length == 8) {
16             int flag = 0;
17             for(int i = 0; i < len-1; i++) {
18                 if(x[i].length() != 4) {
19                     result = "Error";
20                     break;
21                 }
22                 if(x[i].equals("0000")) {
23                     flag++;
24                 }
25             }
26             if(flag == 7 && x[7].equals("0001"))
27                 result = "Loopback";
28             if(flag == 7 && x[7].equals("0000"))
29                 result = "GlobalUnicast";
30             
31             if(x[0].charAt(0)=='F' && x[0].charAt(1)=='E' && x[0].charAt(2)=='8' || 
32                x[0].charAt(0)=='F' && x[0].charAt(1)=='E' && x[0].charAt(2)=='9' ||
33                x[0].charAt(0)=='F' && x[0].charAt(1)=='E' && x[0].charAt(2)=='A' ||
34                x[0].charAt(0)=='F' && x[0].charAt(1)=='E' && x[0].charAt(2)=='B')
35                 result = "LinkLocal";
36             else if(x[0].charAt(0)=='F' && x[0].charAt(1)=='E' && x[0].charAt(2)=='C' || 
37                     x[0].charAt(0)=='F' && x[0].charAt(1)=='E' && x[0].charAt(2)=='D' ||
38                     x[0].charAt(0)=='F' && x[0].charAt(1)=='E' && x[0].charAt(2)=='E' ||
39                     x[0].charAt(0)=='F' && x[0].charAt(1)=='E' && x[0].charAt(2)=='F')
40                 result = "SiteLocal";
41             else if(x[0].charAt(0)=='F' && x[0].charAt(1)=='F') {
42                 result = "Multicast";
43             } else {
44                 result = "Unspecified";
45             }
46         } else {
47             
48         }
49         return result;
50     }
51     
52     public static void main(String[] args) {
53         Scanner sc = new Scanner(System.in);
54         while(sc.hasNext()) {
55             String str = sc.nextLine();
56             System.out.println(solve(str));
57         }
58     }
59 }

 

问题三:按照APP排名顺序,给出下载每个APP需要消耗的流量和可以获得的金币,问在给定流量下,如何下载APP使获得的金币最多,下载一个APP至多获得一次金币;相同金币下,下载排名高的。AC-100%

思路:0-1背包问题,需要记录路径,可以用一个二维数组,相同情况下选择排名高的处理方式为:从后往前遍历,遇见价值大于或者相同的都更新,即 >= 时更新

 1 import java.util.ArrayList;
 2 import java.util.Collections;
 3 import java.util.Scanner;
 4 
 5 public class Main3 {
 6     public static void main(String[] args) {
 7         Scanner sc = new Scanner(System.in);
 8         while(sc.hasNext()) {
 9             int M = sc.nextInt();
10             sc.nextLine();
11             String appInput = sc.nextLine();
12             String coinInput = sc.nextLine();
13             String[] apptmp = appInput.split("\\s+");
14             String[] cointmp = coinInput.split("\\s+");
15             int N = apptmp.length;
16             int[] app = new int[N+1];
17             int[] coin = new int[N+1];
18             
19             for(int i = 1; i <= N; i++) {
20                 app[i] = Integer.parseInt(apptmp[i-1]);
21             }
22             for(int i = 1; i <= N; i++) {
23                 coin[i] = Integer.parseInt(cointmp[i-1]);
24             }
25             
26             int[] dp = new int[M+1];
27             int[][] path = new int[N+1][M+1];
28             for(int i = 1; i <= N; i++) {
29                 for(int j = M; j >= 0; j--) {
30                     if(j >= app[i]) {
31                         if(dp[j] < dp[j-app[i]] + coin[i]) {
32                             dp[j] = dp[j-app[i]] + coin[i];
33                             path[i][j] = 1;
34                         }
35                     }
36                 }
37             }
38             ArrayList<Integer> ans = new ArrayList<>();
39             int x = N, y = M;
40             while(x > 0 && y > 0) {
41                 if(path[x][y] == 1) {
42                     ans.add(x);
43                     y -= app[x];
44                 }
45                 x--;
46             }
47             Collections.sort(ans);
48             for(int i = 0; i < ans.size(); i++) {
49                 if(i == 0)
50                     System.out.print(ans.get(i));
51                 else
52                     System.out.print(" " + ans.get(i));
53             }
54             System.out.println();
55         }
56     }
57 }

 

posted @ 2014-07-29 17:15  water160  阅读(487)  评论(0编辑  收藏  举报