天天看點

Codeforces Round #604 (Div. 2) 部分題解

                               A. Beautiful String

A string is called beautiful if no two consecutive characters are equal. For example, "ababcb", "a" and "abab" are beautiful strings, while "aaaaaa", "abaa" and "bb" are not.

Ahcl wants to construct a beautiful string. He has a string ss, consisting of only characters 'a', 'b', 'c' and '?'. Ahcl needs to replace each character '?' with one of the three characters 'a', 'b' or 'c', such that the resulting string is beautiful. Please help him!

More formally, after replacing all characters '?', the condition si≠si+1si≠si+1 should be satisfied for all 1≤i≤|s|−11≤i≤|s|−1, where |s||s| is the length of the string ss.

Input

The first line contains positive integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Next tt lines contain the descriptions of test cases.

Each line contains a non-empty string ss consisting of only characters 'a', 'b', 'c' and '?'.

It is guaranteed that in each test case a string ss has at least one character '?'. The sum of lengths of strings ss in all test cases does not exceed 105105.

Output

For each test case given in the input print the answer in the following format:

  • If it is impossible to create a beautiful string, print "-1" (without quotes);
  • Otherwise, print the resulting beautiful string after replacing all '?' characters. If there are multiple answers, you can print any of them.

Example

input

Copy

3
a???cb
a??bbc
a?b?c      

output

Copy

ababcb
-1
acbac      

Note

In the first test case, all possible correct answers are "ababcb", "abcacb", "abcbcb", "acabcb" and "acbacb". The two answers "abcbab" and "abaabc" are incorrect, because you can replace only '?' characters and the resulting string must be beautiful.

In the second test case, it is impossible to create a beautiful string, because the 44-th and 55-th characters will be always equal.

In the third test case, the only answer is "acbac".

    題意很簡單,給定一個隻含a,b,c,?的字元串,如果字元串出現連續相同的字母(!='?'),就是輸出-1,否則把?處填上字母,保證不會出現連續相同字母

    自己寫的實在是太麻煩,把幾種情況都考慮進去了:  1:????  2:a?????  3:??????a  對比其他人的代碼,感覺自己真是蠢,還是菜!

    先上自己的渣渣代碼:

    

#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e5+10;
char s[maxn];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s;
        int len = strlen(s);
        int ok = 0 ,ok2=0;
        for(int i = 0 ; i < len; i++)
        {
            if(s[i]==s[i+1]&&s[i]!='?')
                ok=1;
            if(s[i]!='?')
                ok2=1;
        }
        if(!ok2)
        {
            s[0]='a';
            for(int i = 1 ; i < len ; i++)
                {
                    if(s[i-1]=='c')
                        s[i]='a';
                    else
                        s[i]=s[i-1]+1;
                }
            cout<<s<<endl;
            continue ; 
        }
        if(ok)
        {
            cout<<"-1"<<endl;
            continue;
        }        
        for(int i = 0 ; i< len;i++)
        {
            if(s[i]=='?')
            {
                int l=i;
                int r;
                for(int j = i;j<len;j++)
                {
                    if(s[j]!='?')
                        break;
                    r=j;
                //    cout<<r<<endl;
                }    
                //cout<<r<<"  "<<len<<endl;    
                if(i==0)
                {
                    for(int j=r;j>=l;j--)
                    {
                        if(s[j+1]=='a')
                            s[j]='c';
                        else
                            s[j]=s[j+1]-1;
                    }
                }
                else if(r==len-1)
                {
                    
                    for(int j=l;j<=r;j++)
                    {
                        if(s[j-1]=='c')
                            s[j]='a';
                        else
                            s[j]=s[j-1]+1;    
                    }    
                }            
                else
                {
                    for(int j=l;j<r;j++)
                    {
                        if(s[j-1]=='c')    
                            s[j]='a';
                        else
                            s[j]=s[j-1]+1;
                    }
                    if(s[r-1]=='a'&&s[r+1]=='a'||s[r-1]=='a'&&s[r+1]=='b'||s[r-1]=='b'&&s[r+1]=='a')
                        s[r]='c';
                    else if(s[r-1]=='b'&&s[r+1]=='b'||s[r-1]=='b'&&s[r+1]=='c'||s[r-1]=='c'&&s[r+1]=='b'||s[r-1]=='c'&&s[r+1]=='c')
                        s[r]='a';
                    else
                        s[r]='b';
                }
            }
        }
        cout<<s<<endl;
    }
}      

    賽後參考别人然後自己寫的代碼:

    

#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e5+10;
char s[maxn];
int main()
{
    int t;
    cin>>t;
    while(t--)    
    {
        cin>>s;
        int len=strlen(s);
        int ok = 0;
        for(int i =0 ; i< len ; i++)
        {
            if(s[i]==s[i+1]&&s[i]!='?')
            {
                ok=1;break;
            }
            if(s[i]=='?')
            {
                for(int j=0;j<3;j++)
                {
                    if('a'+j!=s[i-1]&&'a'+j!=s[i+1])  //管他?在什麼位置,直接中間不能于兩邊就上就行了。
                    {
                        s[i]='a'+j;break;
                    }
                    
                }
            }
        }
        if(ok)
            cout<<"-1"<<endl;
        else 
        cout<<s<<endl;
    }
}      

                                 B-Beautiful Numbers

  

You are given a permutation p=[p1,p2,…,pn]p=[p1,p2,…,pn] of integers from 11 to nn. Let's call the number mm (1≤m≤n1≤m≤n) beautiful, if there exists two indices l,rl,r (1≤l≤r≤n1≤l≤r≤n), such that the numbers [pl,pl+1,…,pr][pl,pl+1,…,pr] is a permutation of numbers 1,2,…,m1,2,…,m.

For example, let p=[4,5,1,3,2,6]p=[4,5,1,3,2,6]. In this case, the numbers 1,3,5,61,3,5,6 are beautiful and 2,42,4 are not. It is because:

  • if l=3l=3 and r=3r=3 we will have a permutation [1][1] for m=1m=1;
  • if l=3l=3 and r=5r=5 we will have a permutation [1,3,2][1,3,2] for m=3m=3;
  • if l=1l=1 and r=5r=5 we will have a permutation [4,5,1,3,2][4,5,1,3,2] for m=5m=5;
  • if l=1l=1 and r=6r=6 we will have a permutation [4,5,1,3,2,6][4,5,1,3,2,6] for m=6m=6;
  • it is impossible to take some ll and rr, such that [pl,pl+1,…,pr][pl,pl+1,…,pr] is a permutation of numbers 1,2,…,m1,2,…,m for m=2m=2 and for m=4m=4.

You are given a permutation p=[p1,p2,…,pn]p=[p1,p2,…,pn]. For all mm (1≤m≤n1≤m≤n) determine if it is a beautiful number or not.

Input

The first line contains the only integer tt (1≤t≤10001≤t≤1000)  — the number of test cases in the input. The next lines contain the description of test cases.

The first line of a test case contains a number nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the given permutation pp. The next line contains nnintegers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n, all pipi are different) — the given permutation pp.

It is guaranteed, that the sum of nn from all test cases in the input doesn't exceed 2⋅1052⋅105.

Output

Print tt lines — the answers to test cases in the order they are given in the input.

The answer to a test case is the string of length nn, there the ii-th character is equal to 11 if ii is a beautiful number and is equal to 00 if ii is not a beautiful number.

Example

input

Copy

3
6
4 5 1 3 2 6
5
5 3 1 2 4
4
1 4 3 2      

output

Copy

101011
11111
1001      

Note

The first test case is described in the problem statement.

In the second test case all numbers from 11 to 55 are beautiful:

  • if l=3l=3 and r=3r=3 we will have a permutation [1][1] for m=1m=1;
  • if l=3l=3 and r=4r=4 we will have a permutation [1,2][1,2] for m=2m=2;
  • if l=2l=2 and r=4r=4 we will have a permutation [3,1,2][3,1,2] for m=3m=3;
  • if l=2l=2 and r=5r=5 we will have a permutation [3,1,2,4][3,1,2,4] for m=4m=4;
  • if l=1l=1 and r=5r=5 we will have a permutation [5,3,1,2,4][5,3,1,2,4] for m=5m=5.

    題意:給出n,  n個數, 1~n。問這個排列是不是有x,區間長度為x,之内是1~x的全排列。呃是不是不太明了,看樣例。

    比如樣例3:  1  4  3  2

        idx:  1  2  3  4

     l=1,r=1,  n= 1,可以。

     對于2:要想有1,2  idx處是1~4,很明顯多了其他的數,不是1~2的全排列。

    對于3:  要想有1,2,3,idx處是1~4,4個元素,肯定不行。

    對于4:1~4,可以了。

    對于這個題,暴力會逾時的,是以我們從l,r入手,不斷更新l,r,即代碼中的,minn,maxx。一定讓區間包括1~x,如果需要闊R,就更新maxx,闊L,更新minn:

max(maxx,idx[i]);
            minn=min(minn,idx[i]);    //idx記錄坐标      

    如果maxx-minn+1(表示區間内數字的數目)比需要全排列的x(x的全排列就是x個)還大,那肯定不行,是以需要maxx-minn+1==x

 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 2e5+10;
const int inf = 1e9;
int idx[maxn];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int x;
        for(int i = 1;i<=n;i++ )
        {
            cin>>x;
            idx[x]=i;
        }
        int maxx=-1;
        int minn = inf;
        for(int i=1;i<=n;i++)
        {
            maxx=max(maxx,idx[i]);
            minn=min(minn,idx[i]);
            if((maxx-minn+1)==i)
                cout<<"1";
            else
                cout<<"0";
        }
        cout<<endl;
    }
}