编程学习--刷题

C. Find and Replace

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

You are given a string s consisting of lowercase Latin characters. In an operation, you can take a character and replace all occurrences of this character with 00 or replace all occurrences of this character with 11.

Is it possible to perform some number of moves so that the resulting string is an alternating binary string††?

For example, consider the string abacabaabacaba. You can perform the following moves:

  • Replace aa with 00. Now the string is 0b0c0b00b0c0b0.

  • Replace bb with 11. Now the string is 010c010010c010.

  • Replace cc with 11. Now the string is 01010100101010. This is an alternating binary string.

††An alternating binary string is a string of 00s and 11s such that no two adjacent bits are equal. For example, 0101010101010101, 101101, 11 are alternating binary strings, but 01100110, 0a0a00a0a0, 1010010100 are not.

Input

The input consists of multiple test cases. The first line contains an integer t (1≤t≤1001≤�≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer n (1≤n≤20001≤�≤2000) — the length of the string s.

The second line of each test case contains a string consisting of n lowercase Latin characters — the string s.

Output

For each test case, output "YES" (without quotes) if you can make the string into an alternating binary string, and "NO" (without quotes) otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

Example

input

Copy

87abacaba2aa1y4bkpt6ninfia6banana10codeforces8testcase

output

Copy

YES

NO

YES

YES

NO

YES

NO

NO

Note

The first test case is explained in the statement.

In the second test case, the only possible binary strings you can make are 0000 and 1111, neither of which are alternating.

In the third test case, you can make 11, which is an alternating binary string.

简单翻译:给你一个字符串S,你可以进行这样一个操作,把相同字母统一换成0或1;问能否通过一系列操作后得到一个相邻数字不相同的字符串

比如:abcba 能换成10101或01010 输出yes

abcab 则不能得到条件字符串 输出no

思路解析:首先定一个搜索方向,从左往右搜索,当前字符必须换成与左边字符相反的数字,比如左边换成了1,那么该字符就必须是0,且与该字符相同的字符都得换成0,于是每次替换的时候,对该字符进行检验,如果两边出现了不一样的数字,则该字符串不能得到目标字符串,输出no;如果替换完之后,没有不满足条件的,则输出yes。

然后从第一个字符开始,令所有该字符为1(或者0,不影响结果),开始以上搜索:

if(i == 0)
            {
                ch = st[0];
                st[0] = '1';
                for(j = i+1;j<n;j++)
                {
                    if(st[j] == ch)
                    {
                        st[j] = '1';
                        if(st[j-1] == st[j] || (j<n-1 && st[j+1] == st[j]))
                        {
                            jug = 0;
                            break;
                        }
                    }
                }

下面附上AC代码:

#include <iostream> 
 
using namespace std;
 
const int N = 2e5+5;
 
int main()
{
    int t,n,j;
    string st;
    char ch;
 
    cin>>t;
    while(t--)
    {
        cin>>n;
        cin>>st;
 
        int jug = 1;
 
        for(int i = 0;i<n;i++)
        {
            if(i == 0)
            {
                ch = st[0];
                st[0] = '1';
                for(j = i+1;j<n;j++)
                {
                    if(st[j] == ch)
                    {
                        st[j] = '1';
                        if(st[j-1] == st[j] || (j<n-1 && st[j+1] == st[j]))
                        {
                            jug = 0;
                            break;
                        }
                    }
                }
            }
            else
            {
                if(st[i-1] == '1')
                {
                    if(st[i+1] == '0')
                    {
                        jug = 0;
                        break;
                    }
                    else
                    {
                        ch = st[i];
                        st[i] = '0';
                        for(j = i+1;j<n;j++)
                        {
                            if(st[j] == ch)
                            {
                                st[j] = '0';
                                 if(st[j-1] == st[j] || (j<n-1 && st[j+1] == st[j]))
                                {
                                    jug = 0;
                                    break;
                                }
                            }
                        }
                    }
                }
                else if(st[i-1] == '0')
                {
                    if(st[i+1] == '1')
                    {
                        jug = 0;
                        break;
                    }
                    else
                    {
                        ch = st[i];
                        st[i] = '1';
                        for(j = i+1;j<n;j++)
                        {
                            if(st[j] == ch)
                            {
                                st[j] = '1';
                                if(st[j-1] == st[j] || (j<n-1 && st[j+1] == st[j]))
                                {
                                    jug = 0;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
 
        if(jug == 1)
        {
            cout<<"yes"<<endl;
        }
        else
        {
            cout<<"no"<<endl;
        }
    }
 
    return 0;
}

思路拓展:也许可以直接跟据左边的元素进行替换,最后再检查相邻是否一致......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值