0 of 1 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 1 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
阅读程序,根据输入写出输出结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <iostream> #include <string> using namespace std; int lefts[20], rights[20], father[20]; string s1, s2, s3; int n, ans; void calc(int x, int dep){ ans = ans + dep*(s1[x] - 'A' + 1); if (lefts[x] >= 0) calc(lefts[x], dep+1); if (rights[x] >= 0) calc(rights[x], dep+1); } void check(int x){ if (lefts[x] >= 0) check(lefts[x]); s3 = s3 + s1[x]; if (rights[x] >= 0) check(rights[x]); } void dfs(int x, int th){ if (th == n){ s3 = ""; check(0); if (s3 == s2){ ans = 0; calc(0, 1); cout<<ans<<endl; } return; } if (lefts[x] == -1 && rights[x] == -1){ lefts[x] = th; father[th] = x; dfs(th, th+1); father[th] = -1; lefts[x] = -1; } if (rights[x] == -1){ rights[x] = th; father[th] = x; dfs(th, th+1); father[th] = -1; rights[x] = -1; } if (father[x] >= 0) dfs(father[x], th); } int main(){ cin>>s1; cin>>s2; n = s1.size(); memset(lefts, -1, sizeof(lefts)); memset(rights, -1, sizeof(rights)); memset(father, -1, sizeof(father)); dfs(0, 1); } |
输入:
ABCDEF
BCAEDF
输出:
(2012年提高组)