Hlg 【表达式求值+欧拉路径】.cpp

题意:

  给出一些表达式..求出表达式的和..

  然后判断这些表达式是否可以组成一串首尾相连的数字串..

  P.S. 如果有多组答案则按字典序给出..

 

思路:

  栈的应用求出表达式..

  深搜求欧拉路径..

  

Tips:

  如果有可行解则在深搜过程到尾的时候就输出~

  否则的话因为是栈的应用~可能返回的过程中会导致答案的更改..

 

Code:

View Code
  1 #include <stdio.h>

  2 #include <cstring>

  3 #include <iostream>

  4 #include <algorithm>

  5 #include <ctype.h>

  6 #include <map>

  7 using namespace std;

  8 

  9 struct Node

 10 {

 11     char arr[1024];

 12     int st;

 13     int en;

 14 }node[1024];

 15 

 16 const int MAXN = 40;

 17 int com[250][250];

 18 

 19 void ini()

 20 {

 21     memset(com, 0xff, sizeof(com));

 22     com['+']['+'] = com['+']['-'] = com['+'][')'] = com['+']['#'] = 1;

 23     com['-']['-'] = com['-']['+'] = com['-'][')'] = com['-']['#'] = 1;

 24     com['*']['+'] = com['*']['-'] = com['*']['*'] = com['*']['/'] = com['*'][')'] = com['*']['#'] = 1;

 25     com['/']['+'] = com['/']['-'] = com['/']['*'] = com['/']['/'] = com['/'][')'] = com['/']['#'] = 1;

 26     com[')']['+'] = com[')']['-'] = com[')']['*'] = com[')']['/'] = com[')'][')'] = com[')']['#'] = 1;

 27     com['('][')'] = com['#']['#'] = 0;

 28 }

 29 

 30 int change(char *&arr)

 31 {

 32     int sum = 0;

 33     while(isdigit(*arr)) {

 34         sum = sum * 10 + (*arr-'0');

 35         arr++;

 36     }

 37     return sum;

 38 }

 39 

 40 int cal(int a, char o, int b)

 41 {

 42     if(o == '+') return a+b;

 43     else if(o == '-') return a-b;

 44     else if(o == '*') return a*b;

 45     else if(o == '/') return a/b;

 46 }

 47 

 48 bool isoptr(char c) {

 49     if(c == '(' || c == '+' || c == '-' || c == '*' || c == '/' || c == ')' || c == '#') return true;

 50     else return false;

 51 }

 52 

 53 int calculate(char *arr)

 54 {

 55     char optr[MAXN];

 56     int opnd[MAXN];

 57     char c;

 58     int a, b;

 59     int top1 = 0, top2 = 0;

 60     optr[top1++] = '#';

 61     while(*arr != '#' || optr[top1-1] != '#') {

 62         if(!isoptr(*arr)) {

 63            opnd[top2++] = change(arr);

 64           // arr++;//!!!

 65         } else {

 66             c = optr[top1-1];

 67             switch(com[c][*arr]) {

 68                 case -1:

 69                     optr[top1++] = *arr;

 70                     arr++;

 71                     break;

 72                 case 0:

 73                     --top1;

 74                     arr++;

 75                     break;

 76                 case 1:

 77                     c = optr[--top1];

 78                     b = opnd[--top2];

 79                     a = opnd[--top2];

 80                     opnd[top2++] = cal(a, c, b);

 81                     break;

 82             }

 83         }

 84     }

 85     return opnd[top2-1];

 86 }

 87 

 88 bool vis[1010];

 89 int val[1010];

 90 int stack[1010], top;

 91 bool flag;

 92 int n;

 93 

 94 void dfs(int u, int tot) {

 95     if(flag)

 96         return;

 97     if(tot == n) {

 98         flag = true;

 99         for(int i = 0; i < n; ++i) {

100             printf("%d%c", val[stack[i]], i == n-1?'\n':'.');

101         }

102         return;

103     }

104     for(int i = 0; i < n; ++i)

105     if(node[u].en == node[i].st && !vis[i]) {

106         stack[top++] = i;

107         vis[i] = true;

108         dfs(i, tot+1);

109         --top;

110         vis[i] = false;

111     }

112 

113 }

114 bool cmp(Node a, Node b)

115 {

116     return strcmp(a.arr, b.arr)<=0;

117 }

118 int main()

119 {

120     int i, j, k;

121     int T;

122     char arr[100];

123     while(scanf("%d", &T) != EOF)

124     for(k = 1; k <= T; ++k) {

125         scanf("%d", &n);

126 

127         memset(vis, 0, sizeof(vis));

128         top = 0;

129         ini();

130         flag = false;

131 

132         for(i = 0; i < n; ++i) {

133             scanf("%s", arr);

134             val[i] = calculate(arr);

135         }

136 

137         printf("Case #%d:\n", k);

138         for(i = 0; i < n; ++i) {

139             sprintf(node[i].arr, "%d", val[i]);

140             node[i].st = node[i].arr[0]-'0';

141             node[i].en = val[i]%10;

142 

143         }

144 

145         sort(node, node+n, cmp);

146         for(i = 0; i < n; ++i) {

147             vis[i] = true;

148             stack[top++] = i;

149             dfs(i, 1);

150             if(flag) break;

151             vis[i] = false;

152             --top;

153         }

154 

155         if(!flag) puts("NO DIGIT SEQUENCE");

156         puts("");

157 

158     }

159     return 0;

160 }

 

 

题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1523

你可能感兴趣的:(表达式)