hrbust 1523 表达式求值+dfs

Digit Sequence
Time Limit: 1000 MS Memory Limit: 65535 K
     
Description

Dawn is playing a game with some children. He gives them several arithmetic expressions and then asks them whether these expressions' value can form a digit sequence. The value of an expression is just the result we calculate them. For example, the value of expression "3*(7-2)" is 15. It's confirmed that each expression contains only '+', '-', '*', '/', '(', ')' characters. And the numbers in it are in the range from 0 to 2^31-1 which means there are no negative numbers. It is also guaranteed that the value of each arithmetic expression a non-negative number. If the expression contains 'A/B' character, you can assume that 'A' can always be divided with no remainder by 'B'.

But what is the digit sequence? A digit sequence is defined as such a number queue which a former number's last digit is same to the later number's first digit. For examlpe, giving four numbers 123, 675, 398, 86 , They can form a digit sequence 123-398-86-675. On the other hand, if gives numbers 11,33, we can't get it.

So the problem dawn leaving for the children is if the value of each expression can make up a digit sequence. Be sure that children must use all of the expressons' value. Since this is a litle difficult for children, can you write a program to help them. And if you can help children solve this problem, Dawn will give each child an ice cream including you.

Input

The first line of standard input contains an integer T (T <= 10), the number of test cases.

Each test case begins with N (1 <= N <= 1000) - the number of arithmetic expression. Then N lines follow. Each line contains an arithmetic expression ended with a '#' which has been described above. The total characters in each arithmetic expression will not exceed 30. Note that the value of each arithmetic expression will in the range of 32-bit unsigned integer.

Output

For each test case, first you should print “Case #x:” in a line, where x stands for the case number started with 1. Then output a line giving the smallest digit sequence made up of the value of each expression given in the input, every two numbers are seperated by a '.', or "NO DIGIT SEQUENCE" if there is no solution.

Output a blank line after each test case, even the last one.

Sample Input

3

1

3*(2+8)#

3

(3+7-4)*(3-1)#

25*2+1#

100/2-25#

2

11#

11*3#

Sample Output

Case #1:

30

 

Case #2:

12.25.51

 

Case #3:

NO DIGIT SEQUENCE

 

Hint

Attention, if there are more than one solution, you must output the smallest one.

In the first case, there is only one expression, and just output what value the expression is.

In the Second case, the value of each expression is 12, 51, 25. And you may get 3 types of digit sequence which are 122551, 255112, 511225, but 122551 is the smallest one.

In the Third case, the first value is 11 and the second one is 33. They can't make up the digit sequence.

思路:表达式求值+dfs输出最小字典序(个人觉得是判断有没有欧拉通路或者欧拉回路,输出最小字典序,dfs的挺暴力的)

View Code
  1 #include <string.h>
  2 #include <stdio.h>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6 using namespace std;
  7 int s1[100];
  8 char s2[100];
  9 int t1,t2;
 10 void calcu()
 11 {
 12     int x1,x2,x;
 13     char p;
 14     p=s2[t2--];
 15     x2=s1[t1--];
 16     x1=s1[t1--];
 17     switch(p)
 18     {
 19         case '+':x=x1+x2;break;
 20         case '-':x=x1-x2;break;
 21         case '*':x=x1*x2;break;
 22         case '/':x=x1/x2;
 23     }
 24     s1[++t1]=x;
 25 }
 26 int calculator(char *f)
 27 {
 28      int v,i=0;
 29      char *p=f;
 30      t1=t2=0;
 31      while(*p!='#')
 32      switch(*p)
 33      {
 34          case '+':
 35          case '-':
 36                   while (t2&&(s2[t2]!='('))
 37                   calcu();
 38                   s2[++t2]=*p;
 39                   p++;
 40          break;
 41          case '*':
 42          case '/':
 43                   if (t2&&(s2[t2]=='*')||(s2[t2]=='/'))
 44                   calcu();
 45                   s2[++t2]=*p;
 46                   p++;
 47          break;
 48          case '(':
 49                   s2[++t2]=*p;
 50                   p++;
 51          break;
 52          case ')':
 53                   while (s2[t2]!='(')
 54                   calcu();
 55                   t2--;
 56                   p++;
 57          break;
 58          default:
 59                   v=0;
 60                   do {
 61                         v=10*v+*p-'0';
 62                         p++;
 63                      }
 64                   while((*p>='0')&&(*p<='9'));
 65                   s1[++t1]=v;
 66      };
 67      while (t2) calcu();
 68      return s1[t1];
 69 }
 70 char a[1010];
 71 bool v[1010];
 72 int num[1010],n;
 73 string res[1010],ans[1010];
 74 string change(int x)
 75 {
 76     string s="";
 77     while(x>=10)
 78     {
 79         s=(char)(x%10+'0')+s;
 80         x/=10;
 81     }
 82     s=(char)(x%10+'0')+s;
 83     return s;
 84 }
 85 int dfs(int f,int cnt)
 86 {
 87     int len;
 88     if(cnt==n)
 89       return 1;
 90     for(int i=0;i<n;i++)
 91     {
 92         if(!v[i])
 93         {
 94             len=res[f].length();
 95             if(res[f][len-1]==res[i][0])
 96             {
 97                 v[i]=1;
 98                 if(dfs(i,cnt+1))
 99                 {
100                     ans[cnt]=res[i];
101                     return 1;
102                 }
103                 v[i]=0;
104             }
105         }
106     }
107     return 0;
108 }
109 int main()
110 {
111     int T;
112     scanf("%d",&T);
113     for(int tt=1;tt<=T;tt++)
114     {
115         scanf("%d",&n);
116         for(int i=0;i<n;i++)
117         {
118             scanf("%s",a);
119             num[i]=calculator(a);
120             res[i]=change(num[i]);
121         }
122         printf("Case #%d:\n",tt);
123         sort(res,res+n);
124         int flag=0;
125         for(int i=0;i<n;i++)
126         {
127             memset(v,0,sizeof(v));
128             v[i]=1;
129             ans[0]=res[i];
130             if(dfs(i,1))
131             {
132                 flag=1;
133                 for(i=0;i<n;i++)
134                 {
135                     if(i!=0)
136                     printf(".");
137                     cout<<ans[i];
138                 }
139                 break;
140             }
141         }
142         if(!flag)
143         printf("NO DIGIT SEQUENCE");
144         printf("\n\n");
145     }
146     return 0;
147 }

 

posted @ 2012-08-18 11:25  _sunshine  阅读(289)  评论(0编辑  收藏  举报