码迷,mamicode.com
首页 > 其他好文 > 详细

【HDOJ6342】Expression in Memories(模拟)

时间:2018-08-02 13:15:25      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:false   bsp   ret   oss   sum   ESS   技术分享   ack   first   

题意:

给定一个由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 }

 

【HDOJ6342】Expression in Memories(模拟)

标签:false   bsp   ret   oss   sum   ESS   技术分享   ack   first   

原文地址:https://www.cnblogs.com/myx12345/p/9406539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!