HDU 4430 a very hard aoshu problem DFS mathematical formula

Source: Internet
Author: User

After the competition, I was told that it was a DFS question. I had the place where I enumerated =. Then I checked whether the results on both sides were the same, each position on both sides of the equal sign is either placed with + or not. In addition, because 1 + 11 = 12 and 11 + 1 = 12 are two types in the question, you cannot save only the result 12 on the left of the equal sign, but also the number of 12, I saved it directly using a vector, and added 1 if the scanning surface has the same content. this DFS was written for two or three hours,CodeThe capability is still too bad.

# Include <iostream> # include <cstdio> # include <cstdlib> # include <vector> # include <cmath> # include <set> # include <cstring> # include <algorithm> # define maxn 500010 using namespace STD; char STR [20]; bool save [20]; int ans, I; set <int> ST1; vector <int> st; void DFS (INT cur, int end, int sum) {If (cur = END) {int temp = cur; while (save [temp] = false & temp> = 0) temp --; int num = 0; For (Int J = ++ temp; j <= cur; j ++) num = num * 10 + STR [J]-'0 '; Sum + = num; ST. push_back (SUM); return;} Save [cur] = true; int num = 0; int temp = cur-1; while (save [temp] = false & temp> = 0) temp --; For (Int J = ++ temp; j <= cur; j ++) num = num * 10 + STR [J]-'0'; sum + = num; DFS (cur + 1, end, sum); save [cur] = false; sum-= num; DFS (cur + 1, end, sum);} void dfs2 (INT cur, int end, int sum) {If (cur = end) {int temp = cur; while (save [temp] = false & temp> I) temp --; int num = 0; For (Int J = ++ temp; j <= cur; J ++) num = num * 10 + STR [J]-'0'; sum + = num; For (Int J = 0; j <(INT) ST. size (); j ++) {If (sum = sT [J]) ans ++;} return;} Save [cur] = true; int num = 0; int temp = cur-1; while (save [temp] = false & temp> I) temp --; For (Int J = ++ temp; j <= cur; j ++) num = num * 10 + STR [J]-'0'; sum + = num; dfs2 (cur + 1, end, sum ); save [cur] = false; sum-= num; dfs2 (cur + 1, end, sum);} int main () {// freopen ("in.txt ", "r", stdin); While (CIN> STR & strcmp (STR, "End ")! = 0) {int Len = strlen (STR); ans = 0; for (I = 0; STR [I + 1]! = '\ 0'; I ++) {St. clear (); memset (save, false, sizeof (SAVE); DFS (0, I, 0); dfs2 (I + 1, len-1, 0 );} cout <ans <Endl;} return 0 ;}


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.