五一训练包 水题

h-8 水题题解:https://vjudge.net/contest/436484#problem/H

题意照样很好理解,但是写这个题emmmmm,使我距离头秃又近了一步。

题意:t组输入,给你俩值n和m为0和1的数量,再给一个长为n+m的字符串,包含01和?,问能不能将?变成0或者1,并且将字符串变为回文字符串,能就输出变换后的字符串,否则-1。

思路:遍历一下字符串,将能变为1的?变成1,0也同理,并记录01的数量,此时判断剩下的01数量,01同为偶数且字符串长为偶数时,可以构成,有一个为奇数时,如果字符串中间为问好,则中间问号变为奇数数量的0或者1,其他情况则不可以,仔细一点好好判断就可以虽然我错了好多好多次,呜呜呜

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int a,b;
 4 char s[400009];
 5 int main(){
 6     int t;
 7     cin>>t;
 8     while(t--){
 9         cin>>a>>b;
10         cin>>s+1;
11         int len=strlen(s+1);
12         int cnt=0;
13         bool flag=false;    //是否是奇数位中心是问号 
14         bool res=true;
15         for(int i=1;i<=(a+b+1)/2;i++){
16             int r= len-i+1;
17             if(s[i]!='?'){
18                 if(s[r]!='?'){
19                     if(s[i]!=s[r]){
20                         res=false;
21                         break;                        
22                     }
23                 }
24                 else s[r]=s[i];                    
25             }
26             else if(s[r]!='?'){
27                     if(s[i]!='?'){
28                         if(s[i]!=s[r]){
29                         res=false;
30                         break;                        
31                     }
32                 }
33                 else s[i]=s[r];                                    
34             }
35             else {
36                 if(((a+b)%2==1)&&i==r) cnt++;
37                 else cnt+=2;
38             }
39         }
40         int cnt0=0,cnt1=0;
41         for(int i=1;i<=a+b;i++){
42             if(s[i]=='0') cnt0++;
43             else if(s[i]=='1') cnt1++;            
44         }
45         if(cnt0>a||cnt1>b) res=false;
46         if(res){
47             cnt0=a-cnt0;
48             cnt1=b-cnt1;
49             for(int i=1;i<=(a+b+1)/2;i++) {
50                 if(s[i]=='?'){
51                     int r=len-i+1;
52                     if(i==r){
53                         if(cnt0) s[i]='0',cnt0--;
54                         else if(cnt1) s[i]='1',cnt1--;
55                     }
56                     else if(cnt0>1){
57                         s[i]=s[r]='0';
58                         cnt0-=2;
59                     }
60                     else if(cnt1>1){
61                         s[i]=s[r]='1';
62                         cnt1-=2;
63                     }                
64                 }
65             }            
66         }
67         if(cnt0||cnt1) res=false;
68         if(!res) cout<<-1<<endl;
69         else cout<<s+1<<endl;
70     }
71  
72     return 0;
73 }

欧克了

posted @ 2021-05-05 18:40  臭小鬼zm  阅读(39)  评论(0编辑  收藏  举报