http://www.elijahqi.win/archives/3660
题目描述
Walking along a riverside, Mino silently takes a note of something.”Time,” Mino thinks aloud.
“What?”
“Time and tide wait for no man,” explains Mino. “My name, taken from the river, always reminds me of this.”
“And what are you recording?”
“You see it, tide. Everything has its own period, and I think I’ve figured out this one,” says Mino with confidence.
Doubtfully, Kanno peeks at Mino’s records.
The records are expressed as a string
s
s of characters ‘0’, ‘1’ and ‘.’, where ‘0’ denotes a low tide, ‘1’ denotes a high tide, and ‘.’ denotes an unknown one (either high or low).
You are to help Mino determine whether it’s possible that after replacing each ‘.’ independently with ‘0’ or ‘1’, a given integer
p
p is not a period of the resulting string. In case the answer is yes, please also show such a replacement to Mino.
In this problem, a positive integer
p
p is considered a period of string
s
s , if for all
1 \leq i \leq \lvert s \rvert - p
1≤i≤∣s∣−p , the
i
i -th and
(i + p)
(i+p) -th characters of
s
s are the same. Here
\lvert s \rvert
∣s∣ is the length of
s
s .
输入输出格式
输入格式:
The first line contains two space-separated integers
n
n and
p
p (
1 \leq p \leq n \leq 2000
1≤p≤n≤2000 ) — the length of the given string and the supposed period, respectively.
The second line contains a string
s
s of
n
n characters — Mino’s records.
s
s only contains characters ‘0’, ‘1’ and ‘.’, and contains at least one ‘.’ character.
输出格式:
Output one line — if it’s possible that
p
p is not a period of the resulting string, output any one of such strings; otherwise output “No” (without quotes, you can print letters in any case (upper or lower)).
输入输出样例
输入样例#1: 复制
10 7
1.0.1.0.1.
输出样例#1: 复制
1000100010
输入样例#2: 复制
10 6
1.0.1.1000
输出样例#2: 复制
1001101000
输入样例#3: 复制
10 9
1……..1
输出样例#3: 复制
No
说明
In the first example,
7
7 is not a period of the resulting string because the
1
1 -st and
8
8 -th characters of it are different.
In the second example,
6
6 is not a period of the resulting string because the
4
4 -th and
10
10 -th characters of it are different.
In the third example,
9
9 is always a period because the only constraint that the first and last characters are the same is already satisfied.
Note that there are multiple acceptable answers for the first two examples, you can print any of them.
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,p,flag[2];char s[2200],s1[2200];
int main(){
freopen("b.in","r",stdin);
scanf("%d%d",&n,&p);scanf("%s",s+1);
bool ans=1;
for (int i=1;i<=n-p;++i){
flag[0]=flag[1]=0;
for (int j=i;j<=n;j+=p){
if (s[j]=='.') flag[0]=flag[1]=1;
else flag[s[j]-'0']=1;
}
if(flag[0]==flag[1]==1) ans=0;
}if (ans) {puts("No");return 0;}
for (int i=1;i<=n-p;++i){
if(s[i]=='.'&&s[i+p]=='.'){
s[i]='0';s[i+p]='1';continue;
}flag[0]=flag[1]=0;bool flag1=0;
if (s[i]!='.') flag[s[i]-'0']=1;
if (s[i+p]!='.') flag[s[i+p]-'0']=1;
if (!flag[0]) flag1=0;else flag1=1;
if (s[i]=='.') s[i]='0'+flag1;
if (s[i+p]=='.') s[i+p]='0'+flag1;
}//printf("%s",s+1);puts("");
for (int i=1;i<=n;++i)
if (s[i]=='.') s[i]='0';
printf("%s",s+1);
return 0;
}