[ACM] HDU 4403 a very hard aoshu problem (DFS brute force search number)

Source: Internet
Author: User

A very hard aoshu Problem

Problem descriptionaoshu is very popular among primary school students. it is mathematics, but much harder than ordinary mathematics for primary school students. teacher Liu is an aoshu teacher. he just comes out with a problem to test his students:

Given a serial of digits, you must put a '=' and none or some '+ 'between these digits and make an equation. please find out how many equations you can get. for example, if the digits serial is "1212", you can get 2 equations, they are "12 = 12" and "1 + 2 = 1 + 2 ". please note that the digits only include 1 to 9, and every '+ 'must have a digit on its left side and right side. for example, "+ 12 = 12", and "1 + + 1 = 2" are illegal. please note that "1 + 11 = 12" and "11 + 1 = 12" are different equations.
 
Inputthere are several test cases. each test case is a digit serial in a line. the length of a serial is at least 2 and no more than 15. the input ends with a line of "end ".
 
Outputfor each test case, output a integer in a line, indicating the number of equations you can get.
 
Sample Input
1212123456661235END
 
Sample output
220
 
Source2012 ACM/ICPC Asia Regional Jinhua online


Solution:

It is still a headache for DFS. I don't know how it runs. Sometimes I am dizzy when I look at the DFS written by others. I have to do more such questions, there was a DFS storm in the last online competition. No ....

The question of this question is to give a string composed of numbers, add an equal sign to it, and any plus sign to make the equation true, and ask how many solutions are there.

Idea for enumeration equal signs appear position, with equ, then 1 <= equ <= len-1, that is, the equal sign after the equ number, followed by the equ number. Then, first, all the possible situations on the left of the DFS equal sign, corresponding to the lsum on each left, and then all possible situations on the right of the DFS equal sign rsum, if rsum = lsum, there is a feasible solution.

Summary: All statuses on the left of the equal sign are searched through DFS. Each status is pushed into the stack, and then the status is obtained from the stack, and then all the statuses on the right of the DFS equal sign are equal, there is a feasible solution.

# Include <iostream> # include <string. h> # include <stdio. h> using namespace STD; char STR [17]; int Val [17] [17]; int Len; int ans; void CAL () {memset (Val, 0, sizeof (VAL); For (INT I = 1; I <= Len; I ++) for (Int J = I; j <= Len; j ++) {for (int K = I; k <= J; k ++) val [I] [J] = Val [I] [J] * 10 + (STR [k]-'0');} void dfsr (INT lsum, int POs, int rsum) // conditions that may occur after the DFS equal sign {If (Pos> Len) {If (rsum = lsum) ans ++; return ;} for (int K = Pos; k <= Len; k ++) // note that <=, Because the last digit also belongs to the dfsr (lsum, k + 1, rsum + val [POS] [k]) behind the equal sign; // note that k + 1} void dfsl (INT equ, int POs, int lsum) may occur on the left of the DFS equal sign, each case corresponds to an lsum (sum on the left of the equal sign) {If (Pos> equ) // when the position of the number on the left of the equal sign is greater than that on the equal sign, search for the dfsr (lsum, equ +); For (int K = Pos; k <= equ; k ++) // note that, equ indicates that the equal sign is added to the dfsl (equ, k + 1, lsum + val [POS] [k]) after the equ number;} int main () {While (CIN> (STR + 1) & STR [1]! = 'E') {ans = 0; Len = strlen (STR + 1); CAL (); int equ; For (equ = 1; equ <= len-1; equ ++) dfsl (equ, 1, 0); cout <ans <Endl;} return 0 ;}


[ACM] HDU 4403 a very hard aoshu problem (DFS brute force search number)

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.