Algorithms: Back-nine Plus a plus sign in the digital strings, find all cases and

topic

Link: https: //ac.nowcoder.com/acm/contest/3286/C

Beef give you a character string containing only S 1 to 9, you can now insert '+' sign constitute a formula between any two numeric characters, and now want you to obtain all the formulas and Results.
NiuNiu gives you a string contains only digit characters '1' to '9', and tell you that you can insert a '+' for any number of times between any two characters to form a formula. Now he wants to know the sum of the results of all the different formulas that can be formed.

Description Input:
Input string S contains only a numeric character (1≤|S|≤10)
of The INPUT IS A digit for the contains only characters String S '. 1' to '. 9' (1≤|S|≤10).

Description Output:
output sum
Print the sum.
Example 1

输入
125
输出
176
说明
可以得到插入的结果:
125(不插入)
1 + 25 =26
12 + 5= 17
1 + 2 + 5 = 8
所以答案为: 125 + 26 + 17 + 8 = 176
All the possible formulas:
125 (insert no ‘+)
1 + 25 = 26
12 + 5 = 17
1 + 2 + 5 = 8
So the answer is 125 + 26 + 17 + 8 = 176.

Example 2

输入
9999999999
输出
12656242944

DFS backtracking depth-first algorithm

Ideas:

  1. The input character string input, converted to a character array;
  2. Backtracking recursive manner, string concatenation there are two choices, either with +a plus sign, with or without +a plus sign;
  3. Exit conditions, when the number of characters to the penultimate digit, hand stitching the last character, into the list of results resultList.
  4. Traversing the list of results resultListin the string, in accordance with the +plus sign is divided into a number string, the number string can be converted to digital accumulation.
package backtracking;

import java.util.ArrayList;
import java.util.List;

public class AddPlusSymbol {

  public static void main(String[] args) {
    AddPlusSymbol obj = new AddPlusSymbol();
    String input1 = "125";
    Long result1 = obj.addPlus(input1);
    System.out.println("input > " + input1 + " ; result > " + result1);
    String input2 = "9999999999";
    Long result2 = obj.addPlus(input2);
    System.out.println("input > " + input2 + " ; result > " + result2);
  }

  public long addPlus(String input) {
    if (input == null || input.length() == 0) {
      return 0;
    }

    long result = 0;
    List<String> resultList = new ArrayList<String>();
    char[] chars = input.toCharArray();
    // dfs
    dfs(chars, resultList, "", 0);

    // loop list sum
    for (String s: resultList) {
      String[] nums = s.split("\\+");
      for (String n: nums) {
        result += Long.parseLong(n);
      }
    }

    return result;
  }

  private void dfs(char[] chars, List<String> resultList, String s, int start) {
    // exit
    if (start == chars.length - 1) {
      resultList.add(s + chars[chars.length - 1]);
      return;
    }
    s += chars[start];
    dfs(chars, resultList, s + '+', start + 1);
    dfs(chars, resultList, s, start + 1);
  }
}


Download

https://github.com/zgpeace/awesome-java-leetcode/blob/master/code/LeetCode/src/backtracking/AddPlusSymbol.java

Published 127 original articles · won praise 12 · views 20000 +

Guess you like

Origin blog.csdn.net/zgpeace/article/details/103839979