Problem Description
Overlooking the captivating blend of myriads of vernal hues, Arkady the painter lays out a long, long canvas.
Arkady has a sufficiently large amount of paint of three colours: cyan, magenta, and yellow. On the one-dimensional canvas split into n consecutive segments, each segment needs to be painted in one of the colours.
Arkady has already painted some (possibly none or all) segments and passes the paintbrush to you. You are to determine whether there are at least two ways of colouring all the unpainted segments so that no two adjacent segments are of the same colour. Two ways are considered different if and only if a segment is painted in different colours in them.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 100) — the length of the canvas.
The second line contains a string s of n characters, the i-th of which is either 'C' (denoting a segment painted in cyan), 'M' (denoting one painted in magenta), 'Y' (one painted in yellow), or '?' (an unpainted one).
Output
If there are at least two different ways of painting, output "Yes"; otherwise output "No" (both without quotes).
You can print each character in any case (upper or lower).
Examples
| input |
5 CY??Y |
| output |
Yes |
| input |
5 C?C?Y |
| output |
Yes |
| input |
5 ?CYC? |
| output |
Yes |
| input |
5 C??MM |
| output |
No |
| input |
3 MMY |
| output |
No |
Note
For the first example, there are exactly two different ways of colouring: CYCMY and CYMCY.
For the second example, there are also exactly two different ways of colouring: CMCMY and CYCMY.
For the third example, there are four ways of colouring: MCYCM, MCYCY, YCYCM, and YCYCY.
For the fourth example, no matter how the unpainted segments are coloured, the existing magenta segments will prevent the painting from satisfying the requirements. The similar is true for the fifth example.
题意:
给一个长度为n的字符串,如果有两个相邻的字符相等就输出No,把字符串中的?改成CMY,如果有两种及两种以上的改法就输出Yes。
思路:
要考虑全所有的情况再写代码。
代码:
#include <bits/stdc++.h>
using namespace std;
int n,flag;
char s[5001];
int main()
{
scanf("%d",&n);
scanf("%s",s+1);
for(int i=1; i<=n; i++)
{
if((i==1||i==n)&&s[i]=='?')
{
flag=1;
}
if(s[i]=='?'&&(s[i-1]==s[i+1]||s[i-1]=='?'||s[i+1]=='?'))
{
flag=1;
}
if(s[i]==s[i-1]&&s[i]!='?')
{
flag=0;
break;
}
}
if(flag)
printf("Yes\n");
else
printf("No\n");
return 0;
}

博客围绕一个字符串涂色问题展开,给定长度为n的字符串,含'C'、'M'、'Y'和'?',需将'?'改成前三者之一,要求相邻字符不同色。若有两种及以上改法输出'Yes',否则输出'No'。给出了输入输出格式、示例,还提及解题要考虑所有情况并给出代码。
1928

被折叠的 条评论
为什么被折叠?



