Algorithm:
1. Expression operation
2. DFS Enumeration
View code
# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <iostream> # include <vector> # include <string> # include <math. h> # include <map> # include <set> # include <stack> # include <algorithm> using namespace STD; stack <int> ope; // operand stack <char> OPR; // operator stack // int OPET [100], hash [100], len1, len2; bool vis; int visit [100]; char STR [1000]; char temp [110]; // stack top priority int pri [8] [8] = {,-1, -1,-, 1}, {,-1,-1 ,-, 1}, {,-, 1}, {,-, 1}, {-1,-1,-1,-1,-1, -, 0}, {, 1}, {-1,-1,-1,-1,-1,-1,-2 }}; void Init () {memset (OPET, 0, sizeof (OPET); OPET ['+'] = 0, OPET ['-'] = 1, OPET ['*'] = 2; OPET ['/'] = 3, OPET ['('] = 4, OPET [')'] = 5; OPET ['#'] = 6;} // judge the priority int jugde (char P, char q) {return pri [OPET [p] [OPET [Q];} int solve (char * str2) {char str1 [120]; strcpy (str1, str2); int Len = strlen (str1 ), X, Y, I = 0, J; char op = 0, TP [110]; while (! Ope. Empty () Ope. Pop (); While (! OPR. empty () OPR. pop (); OPR. push ('#'); bool F = true; str1 [Len] = '#'; while (I <= Len & F) {If (str1 [I]> = 'A' & str1 [I] <= 'G') // The operands are directly written into the stack {int ans = 0; for (j = I; j <Len; j ++) if (str1 [J]> = 'A' & str1 [J] <= 'G ') TP [ans ++] = hash [str1 [J] + '0'; else break; I = J; TP [ANS] = '\ 0'; Ope. push (atoi (TP);} else {Switch (jugde (OPR. top (), str1 [I]) {Case 0: OPR. pop (); I ++; break; Case 1: {op = OPR. top (); OPR. pop (); X = Ope. top (); Ope. pop (); y = Ope. top (); Ope. pop (); If (OP = '+') Ope. push (x + y); else if (OP = '-') Ope. push (Y-x); else if (OP = '*') Ope. push (y * X); else if (OP = '/') Ope. push (y/X); break;} case-1: OPR. push (str1 [I]); I ++; break; Case-2: F = false; break ;}} return Ope. top ();} void DFS (INT num) {If (VIS) return; If (num = 7) {int x = solve (St R); int y = solve (temp); If (x = y) {vis = true; For (INT I = 0; I <len1; I ++) if (STR [I]> = 'A' & STR [I] <= 'G') printf ("% d", hash [STR [I]); else printf ("% C", STR [I]); printf ("="); For (INT I = 0; I <len2; I ++) if (temp [I]> = 'A' & temp [I] <= 'G') printf ("% d", hash [temp [I]); else printf ("% C", temp [I]); puts ("");} return;} For (INT I = 1; I <= 7; I ++) {If (! Visit [I]) {visit [I] = 1; hash [num + 'a'] = I; DFS (Num + 1); visit [I] = 0; if (VIS) return ;}}int main () {Init (); While (scanf ("% s", STR )! = EOF) {vis = false; memset (visit, 0, sizeof (visit); memset (hash, 0, sizeof (hash); char * P = strtok (STR, "="); strcpy (STR, P); P = strtok (null, "="); strcpy (temp, P); len1 = strlen (STR ); len2 = strlen (temp); DFS (0);} return 0 ;}