其他分享
首页 > 其他分享> > Codeforces Round #798 (Div. 2)A~C

Codeforces Round #798 (Div. 2)A~C

作者:互联网

A.Lex String

Kuznecov likes art, poetry, and music. And strings consisting of lowercase English letters.

Recently, Kuznecov has found two strings, aa and bb, of lengths nn and mm respectively. They consist of lowercase English letters and no character is contained in both strings.

Let another string cc be initially empty. Kuznecov can do the following two types of operations:

But, he can not do more than kk operations of the same type in a row. He must perform operations until either aa or bb becomes empty. What is the lexicographically smallest possible value of cc after he finishes?

A string xx is lexicographically smaller than a string yy if and only if one of the following holds:

Input

There are several test cases in the input data. The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. This is followed by the test cases description.

The first line of each test case contains three integers nn, mm, and kk (1≤n,m,k≤1001≤n,m,k≤100) — parameters from the statement.

The second line of each test case contains the string aa of length nn.

The third line of each test case contains the string bb of length mm.

The strings contain only lowercase English letters. It is guaranteed that no symbol appears in aa and bb simultaneously.

Output

In each test case, output a single string cc — the answer to the problem.

input
3
6 4 2
aaaaaa
bbbb
5 9 3
caaca
bedededeb
7 7 1
noskill
wxhtzdy
output
aabaabaa
aaabbcc
dihktlwlxnyoz

题目大意:给你两个长度分别为n,m的字符串,每次操作选择一个字符串中的一个字符添加到答案的末尾,并在原字符串中删除这个字符,同一个字符串最多连续选择k次。当任意一个字符串为空时停止操作,求最小字典序的答案字符串。

思路:由于题目规定了两个字符串没有相同的字符,所以直接暴力即可。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

int t,n,m,k;
string a,b;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n>>m>>k;
        cin>>a>>b;
        sort(a.begin(),a.end());
        sort(b.begin(),b.end());
        int cnt=0;
        for(int i=0,j=0;i<n&&j<m;){
            if(cnt==k){
                cout<<b[j++];
                cnt=min(cnt,0)-1;
                continue;
            }else if(cnt==-k){
                cout<<a[i++];
                cnt=max(0,cnt)+1;
                continue;
            }
            if(a[i]<b[j]){
                cout<<a[i++];
                cnt=max(0,cnt)+1;
            }else{
                cout<<b[j++];
                cnt=min(0,cnt)-1;
            }
        }
        cout<<'\n';
    }
}

B. Mystic Permutation 

 

Monocarp is a little boy who lives in Byteland and he loves programming.

Recently, he found a permutation of length nn. He has to come up with a mystic permutation. It has to be a new permutation such that it differs from the old one in each position.

More formally, if the old permutation is p1,p2,…,pnp1,p2,…,pn and the new one is q1,q2,…,qnq1,q2,…,qn it must hold that

p1≠q1,p2≠q2,…,pn≠qn.p1≠q1,p2≠q2,…,pn≠qn.

Monocarp is afraid of lexicographically large permutations. Can you please help him to find the lexicographically minimal mystic permutation?

Input

There are several test cases in the input data. The first line contains a single integer tt (1≤t≤2001≤t≤200) — the number of test cases. This is followed by the test cases description.

The first line of each test case contains a positive integer nn (1≤n≤10001≤n≤1000) — the length of the permutation.

The second line of each test case contains nn distinct positive integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). It's guaranteed that pp is a permutation, i. e. pi≠pjpi≠pj for all i≠ji≠j.

It is guaranteed that the sum of nn does not exceed 10001000 over all test cases.

Output

For each test case, output nn positive integers — the lexicographically minimal mystic permutations. If such a permutation does not exist, output −1−1 instead.

Example input
4
3
1 2 3
5
2 3 4 5 1
4
2 3 1 4
1
1
output
2 3 1
1 2 3 4 5
1 2 4 3
-1

题目大意:给定一个n的全排列a,找出一种字典序最小的排列b使ai!=bi。

解题思路:先按照1~n的顺序排列,与原序列不相同的直接确定,否则与前一项或后一项交换即可。

AC代码:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=1010;
int a[maxn],ans[maxn];
int t,n;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            ans[i]=i;
        }
        for(int i=1;i<=n;i++){
            if(ans[i]!=a[i]) continue;
            if(i!=n) swap(ans[i],ans[i+1]);
            else if(i!=1) swap(ans[i],ans[i-1]);
        }
        int flag = 1;
        for(int i=1;i<=n;i++){
            if(ans[i]==a[i]){
                flag = 0;
                break;
            }
        }
        if(flag)
            for(int i=1;i<=n;i++)
                cout<<ans[i]<<' ';
        else
            cout<<-1;
        cout<<endl;
    }
}

C即以下请移步:

标签:string,nn,int,permutation,Codeforces,798,each,test,Div
来源: https://www.cnblogs.com/cbmango/p/16367141.html