Inserting a plus (DP) dynamic plan into a string

Source: Internet
Author: User

Title: A string with a length of M inserts n plus signs for minimum and. For example, string str= "123456", n=2, Output 12+34+56 and 102, and Output 2 4, which is the plus position. Here is the idea:

The implementation process is mainly the idea of interval DP, wherein the state transfer equation is dp[i][j] = min (dp[k][j-1] + getnum (str.substr (k, i-k)), dp[i][j]);//where dp[i][j] means that the J plus sign is used in 1-i , where I use a struct is mainly to record the position of the plus.

#include <iostream> #include <ctime> #include <vector> #include <string> #include <algorithm > #include <sstream>using namespace std;//converts a string into a corresponding integer such as the Getnum value of the string "123" is 123int getnum (string str) {if ( Str.size () <= 0) return 0;int sum = 0;for (int i = 0; i < str.size (); i++) {sum = ten * sum + (Str[i]-' 0 ');} return sum;} Adding each number in the string, for example "123", is Getnum1 6int getnum1 (string str) {int sum = 0;for (int i = 0; i < str.size (); i++) {sum = sum + ( Str[i]-' 0 ');} return sum;} struct Node{int value;int k;}; int main () {string str = "1234567891"; int n = 3;int m = str.size (); VECTOR&LT;VECTOR&LT;NODE&GT;&GT;DP (M + 1, Vector<node > (n+1)); for (int i = 0, i < m+1; i++) {for (int j = 0; J < n+1; J + +) {dp[i][j].value = 9999999;DP[I][J].K = 0;}} vector<int>recordpos;//record the position of the plus sign dp[0][0].value = 0;int index = 0;for (int i = 0; I <=m; i++) {dp[i][0].value = Getnu M (Str.substr (0,i));} for (int j = 0; J <=n; J + +) {dp[0][j].value = 0;} for (int i = 1; i < m+1; i++) {for (iNT J = 1; j< n+1; J + +) {if (J >= i-1) Dp[i][j].value = getnum1 (str.substr (0, I)), else{for (int k = 1; k <=i; k++) {//dp[i][j] = min (dp[ K][j-1] + getnum (str.substr (k, i-k)), Dp[i][j]),//where dp[i][j] means that the 1-i is using the J Plus if (Dp[i][j].value>dp[k][j-1].value + get Num (Str.substr (k, i-k))) {Dp[i][j].value = Dp[k][j-1].value + Getnum (str.substr (k, i-k));//if (i==m) Recordpos.push_ba CK (k);DP [I][J].K = k;}}}} cout << dp[m][n].value << endl;cout << dp[m][n].k << endl;cout << dp[4][1].k << endl;i NT k = Dp[m][n].k;int y = n;while (k>0 && n>0) {recordpos.push_back (k); y--;m = K;k = DP[M][Y].K;} for (int i = 0; i < recordpos.size (); i++) {cout << recordpos[i] << "";} cout << Endl;cin.get (); return 0;}

Inserting a plus (DP) dynamic plan into a string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.