【HDOJ6342】Expression in Memories(模拟)

题意:

给定一个由0123456789+* ?组成的表达式,其中?可以被改为任意其它字符,问修改问号后是否有方案使得表达式合法

len<=5e2,sumlen<=1e5

思路:

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iostream>
 8 #include <ctype.h>
 9 #include <limits.h>
10 #include <set>
11 #include <map>
12 #include <vector>
13 #include <stack>
14 #include <queue>
15 #include <list>
16 #define left (now<<1)
17 #define right ((now<<1)+1)
18 #define mid ((l+r)>>1)
19 #define fst first
20 #define snd second
21 using namespace std;
22 typedef long long lint;
23 
24 const int MAXN = 5e2 + 10;
25 
26 char str[MAXN];
27 int n;
28 bool ok[MAXN];
29 
30 void init(){
31     for(int i = 0; i < MAXN; ++i){
32         str[i] = '\0';
33     }
34 }
35 
36 int main(){
37     int t; scanf("%d",&t);
38     while(t--){
39         init(); scanf("%s",str); int len = strlen(str); bool zero = false;
40         memset(ok,false,sizeof(ok));
41         for(int i = 0; i < len; ++i){
42             if(str[i] == '?'){
43                 str[i] = '1'; ok[i] = true;
44             }
45         }
46 
47         bool can = true; zero = false;
48         if(str[0] == '+' || str[0] == '*' || str[len - 1] == '+' ||str[len - 1] == '*'){
49             can = false;
50         }
51         for(int i = 0; i < len - 1; ++i){
52             if((str[i] == '+' || str[i] == '*') && (str[i + 1] == '*' || str[i + 1] == '+')){
53                 can = false; break;
54             }
55         }
56         for(int i = 0; i < len; ++i){
57             if(isdigit(str[i]) && zero){
58                 if(ok[i] == false){
59                     can = false; break;
60                 }else{
61                     str[i] = '+';
62                 }
63             }
64             if(str[i] == '0' && (i == 0 || !isdigit(str[i - 1]))){
65                 zero = true;
66             }else{
67                 zero = false;
68             }
69         }
70         if(str[0] == '+' || str[0] == '*' || str[len - 1] == '+' ||str[len - 1] == '*'){
71             can = false;
72         }
73         for(int i = 0; i < len - 1; ++i){
74             if((str[i] == '+' || str[i] == '*') && (str[i + 1] == '*' || str[i + 1] == '+')){
75                 can = false; break;
76             }
77         }
78         if(can){
79             printf("%s\n",str);
80         }else{
81             printf("IMPOSSIBLE\n");
82         }
83     }
84     return 0;
85 }

 

posted on 2018-08-02 12:55  myx12345  阅读(165)  评论(0编辑  收藏  举报

导航