0
点赞
收藏
分享

微信扫一扫

C. Binary String Reconstruction(字符串+思维) Educational Codeforces Round 94 (Rated for Div. 2)

原题链接: ​​https://codeforces.com/contest/1400/problem/C​​

C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_#define
测试样例:

input
3
101110
2
01
1
110
1
output
111011
10
-1

题意: 给你一个结果字符串,要求你找出原来的字符串,若没有,则输出-1。

解题思路: 这道题题意很明显了,我这里再列出关键信息。考虑以下过程:您有一个长度为C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_字符串_02的二进制字符串(每个字符都是0或1的字符串)C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_字符串_03和一个整数C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_字符串_04。您将构建一个由C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_字符串_02个字符组成的新二进制字符串s。选择的字符如下:
如果字符C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_思维_06存在且等于1,则C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_思维_07为1(形式上,如果C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_ios_08C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_#define_09,则C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_#define_10);
如果字符C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_思维_06存在且等于1,则C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_思维_07为1(形式上,如果C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_字符串_13C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_#define_09,则C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_#define_10);
如果上述两个条件均为假,则C. Binary String Reconstruction(字符串+思维)   Educational Codeforces Round 94 (Rated for Div. 2)_思维_07为0。

那么我们的思路在哪?肯定是在结果字符串中动手脚,那么为什么可能找不到原来的字符串?因为有可能结果字符串中的选择是自相矛盾的。OK,我们知道这个之后我们就可以假设这个结果字符串是没有问题的,则如果结果字符串中的位置字符为1,那么就两个条件都为假,这意味着若原字符串中字符存在则为0。 那么我们根据结果字符串即可确定完原字符串中的所有0字符,那么其他的不就是1字符了吗?我们的原字符串也就找到了。

可,这样就解决了吗?显然没有,因为我们是假设结果字符串是没有问题的,那么我们还要去检验字符串有没有问题,由于我们是通过结果字符串的0来推的,那么我们就要通过1来检验。这里就是利用题目中的选择规定来验证的:若两个条件都不满足(即不存在或者存在但不等于1) ,验证完之后我们就可以得出结果输出了。具体看代码:我代码中用了辅助数组vis来记录0的位置,也供检验的时候使用

/
*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,x;
string s;
bool vis[maxn];//判断该下标有没有被确定。
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>s>>x;
int len=s.size(),temp1,temp2;
memset(vis,false,sizeof(vis));
rep(i,0,len-1){
if(s[i]=='0'){
temp1=i-x;
temp2=i+x;
if(temp1>=0){
vis[temp1]=true;
}
if(temp2<len){
vis[temp2]=true;
}
}
}
//通过这一步,我们已经确定了假设的原字符串。
bool flag=false;
rep(i,0,len-1){
if(s[i]=='1'){
temp1=i-x;
temp2=i+x;
if(temp1>=0&&temp2<len&&vis[temp1]&&vis[temp2]){
flag=true;
break;
}
else if(temp1>=0&&temp2>=len){
if(vis[temp1]){
flag=true;
break;
}
}
else if(temp2<len&&temp1<0){
if(vis[temp2]){
flag=true;
break;
}
}
else if(temp1<0&&temp2>=len){
flag=true;
break;
}
}
}
if(flag)
cout<<"-1"<<endl;
else{
rep(i,0,len-1){
if(vis[i])
cout<<0;
else
cout<<1;
}
cout<<endl;

}
}
}
return 0;
}


举报

相关推荐

0 条评论