Huawei's 2020 autumn recruit pen questions

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/smarryw/article/details/98973837

First, the full amount of the character set and character set occupied


Enter a description:

Enter a string of characters comprising the total amount of occupied and character sets, the character set is connected with two @. The character before the @ character set amount for the whole collection, after the @ character set is a set of characters occupied. Occupied character set must be the full amount of characters in the character set. Use commas to separate between a set of characters with character. Represented as a set of characters plus the number of characters, characters with English numbers separated by colons, such as a: 1, represent one a character. Consider only letters characters, case sensitive, positive numbers only consider shaping, the number of not more than 100, if a character did not occupied, still @ identifier, for example a: 3, b: 5, c: 2 @

Output Description:

Available character set. Output carriage return line feed.

Example 1:

Input: a: 3, b: 5, c: 2 @ a: 1, b: 2

Output: a: 2, b: 3, c: 2

Note: the total amount of three characters a, 5 th b, 2 th c. Occupied character set is an a, 2 th b. Since the occupied character set can not be reused, and therefore, the remaining available characters 2 a, 3 th B, 2 c. Therefore, the output a: 2, b: 3, c: 2. Note that the character sequence input to keep the same output. Can not output b: 3, a: 2, c: 2. If a character has all been occupied, no output. For example a: 3, b: 5, c: 2 @ a: 3, b: 2, output b: 3, c: 2.
 

Ideas:

This question is mainly related to the string split, splice. . .

Here is my own code, if any improvement, welcome!

public class HuaWei_08_07 {

    public static void main(String[] args){
      String s="a:3,b:5,c:2@a:3,b:1";
      HuaWei_08_07 hua=new HuaWei_08_07();
      String useStr=hua.UserStrings(s);
      System.out.print(useStr);
    }

    public String UserStrings(String s){
          String[] str=s.split("@");
          if(str.length==1){
              return str[0];
          }

         Map<String,Integer> map=new HashMap<>();
          for(int i=0;i<str.length;i++){
              map=putmap( map,str[i].split(","),i);
          }
          
        StringBuffer buf=new StringBuffer();
        for(String key:map.keySet()){
            if(map.get(key)!=0) {
               buf.append(key+":"+map.get(key)+",");
            }
        }
        String s1=buf.toString();
        return s1.substring(0,s1.length()-1);

    }


    public Map<String,Integer> putmap(Map<String,Integer> map,String[] str,int flag){
        for(int i=0;i<str.length;i++){
            String s2=str[i].split(":")[0];
            int a=Integer.parseInt(str[i].split(":")[1]);
            if(flag==1&&map.containsKey(s2)){
                int b=map.get(s2)-a;
                map.put(s2,b);
            }else{
                map.put(s2,a);
            }
        }

        return map;
    }

}

Two, Trie tree


Subject description:


The following is a Trie tree, circles represent the internal node, the child node points to a range of values ​​between 0-255 for each marker, each internal node has a child up to 256 nodes. Triangles represent leaf nodes, each leaf node stores a value, the root node to all characters on a path between the leaf node to form a complete key.

Enter a description:


A first digital line 1 M represents Labels, HasChild, POUDS array size, followed by three rows represent Labels, HasChild, POUDS contents of the array, separated by spaces. Of the digital line 5 N Values ​​represent the array size, followed by a line array Values ​​represent the content. Digital line 7 represents the Key array size, then 1 bank said key array of characters to look for.

Output Description:


Key value corresponding to the output line, if the key does not exist, the output 0.

Example 1:


Input:

15
115 112 116 97 111 121 114 101 105 112 121 114 102 115 116
0 0 0 1 1 0 1 0 0 0 0 1 1 1 1
1 1 0 1 0 1 1 1 0 0 0 1 1 0 0
8
1 2 3 4 5 6 7 8
3
116 114 112

Output:

7

 

 

Third, the logical computing


Subject description:


And there is common logic calculation (denoted as &); Or (denoted as |); Not (indicated as!). Among them, their priority relationship is Not> And (&)> Or (!) (|).

Enter a description:


1, the middle of the test case without spaces, regardless of space.

2, the test case expression will only appear as characters: "0", "1", "(", ")", "&", "|", "!".

3, test cases are legitimate input to the input, no need to consider the illegal entry.

4, the test case will not exceed 128 characters in length.

5, the brackets may be repeated nested.

E.g:

1 | (1 & 0) Returns: 1

1 & 0 | 0 & 1 Returns: 0

! 0 & 1 | 0 Returns: 1

(! (0 & 1)) | 0 Returns: 1

Output Description:


The final result of the logic operation output: 0 or 1

Example 1:
Input:! (1 & 0) | 0 & 1

Output: 1

Example 2:
Input: (1 & 0) & 0 | 0!

Output: 0
The first solution: call the function

 /**第一种 调用函数*/
    public static  void Pipei(String s){

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        Object result=null;
        try {
            result = engine.eval(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

The second:

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        int k = 0;
        while (input.length() != 1) {
            input = input.replaceAll("!1", "0");
            input = input.replaceAll("!0", "1");

            input = input.replaceAll("1&0", "0");
            input = input.replaceAll("0&1", "0");
            input = input.replaceAll("0&0", "0");
            input = input.replaceAll("1&1", "1");


            input = input.replaceAll("1\\|0", "1");
            input = input.replaceAll("0\\|1", "1");
            input = input.replaceAll("0\\|0", "0");
            input = input.replaceAll("1\\|1", "1");

            input = input.replaceAll("\\(1\\)", "1");
            input = input.replaceAll("\\(0\\)", "0");

            k++;
            if (k > 20) break;
        }
        System.out.println(input);
        sc.close();
    }

 

Guess you like

Origin blog.csdn.net/smarryw/article/details/98973837