A very hard Aoshu problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 537 Accepted Submission (s): 368
Problem Description
Aoshu 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.
Input
There 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 ".
Output
For each test case, output a integer in a line, indicating the number of equations you can get.
Sample Input
1212
12345666
1235
END
Sample Output
2
2
0
Source
2012 ACM/ICPC Asia Regional Jinhua Online
Recommend
Zhoujiaqi2010
Question:
Enter a number to convert the formula "+" to "=". For example, "1212" can be converted to "1 + 2 = 1 + 2 12 = 12". Note that 12 = 21 and 21 = 12 are different.
Ideas:
DFS combines all numbers and divides them into two parts to see how many combinations can be made.
# Include <stdio. h> # include <string. h> int cnt, flag [22], num [22], len; char s [22]; /// flag [I] = 1 indicates that a number including I before I is merged into a number and separated into a number to void DFS (int id) {int I, j, k; if (id = len-1) {int c = 0, mid = 0; for (I = 0; I <len; I ++) {if (flag [I] = 1) {mid = mid * 10 + s [I]-'0'; num [c ++] = mid; mid = 0;} else {mid = mid * 10 + s [I]-'0';} if (mid! = 0) num [c ++] = mid; int left = 0, right = 0; for (k = 0; k <c; k ++) {// printf ("num [% d] = % d \ n", k, num [k]); left = right = 0; for (I = 0; I <= k; I ++) left + = num [I]; for (I = k + 1; I <c; I ++) right + = num [I]; if (left = right) cnt ++;} return;} flag [id] = 1; DFS (id + 1 ); flag [id] = 0; DFS (id + 1);} int main () {while (scanf ("% s", s )! = EOF) {if (strcmp (s, "END") = 0) break; len = strlen (s); memset (flag, 0, sizeof (flag )); cnt = 0; DFS (0); printf ("% d \ n", cnt);} return 0 ;}