#include#include using namespace std;int lefts[20], rights[20], father[20]; string s1, s2, s3;int n, ans;void calc(int x, int dep){\tans = ans dep*(s1[x] - 'A' 1);\tif (lefts[x] >= 0) calc(lefts[x], dep 1); \tif (rights[x] >= 0) calc(rights[x], dep 1);}void check(int x){\tif (lefts[x] >= 0) check(lefts[x]); \ts3 = s3 s1[x];\tif (rights[x] >= 0) check(rights[x]);}void dfs(int x, int th){\tif (th == n)\t{\t\ts3 = "";\t\tcheck(0);\t\tif (s3 == s2)\t\t{\t\t\tans = 0;\t\t\tcalc(0, 1); \t\t\tcout< = 0)\t\tdfs(father[x], th);}int main(){\tcin>>s1; \tcin>>s2;\tn = s1.size();\tmemset(lefts, -1, sizeof(lefts));\tmemset(rights, -1, sizeof(rights));\tmemset(father, -1, sizeof(father));\tdfs(0, 1);}
输入:
ABCDEF
BCAEDF
输出:
发表评论 取消回复