CodeForces - 1617A Forbidden Subsequence文本处理+计数排序

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 1617A Forbidden Subsequence文本处理+计数排序相关的知识,希望对你有一定的参考价值。

A. Forbidden Subsequence
time limit per test1 second
memory limit per test256 megabytes

You are given strings S and T, consisting of lowercase English letters. It is guaranteed that T is a permutation of the string abc.

Find string S′, the lexicographically smallest permutation of S such that T is not a subsequence of S′.

String a is a permutation of string b if the number of occurrences of each distinct character is the same in both strings.

A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) elements.

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

  • a is a prefix of b, but a≠b;
  • in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.

Input
Each test contains multiple test cases. The first line contains a single integer t (1≤t≤1000) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a string S (1≤|S|≤100), consisting of lowercase English letters.

The second line of each test case contains a string T that is a permutation of the string abc. (Hence, |T|=3).

Note that there is no limit on the sum of |S| across all test cases.

Output
For each test case, output a single string S′, the lexicographically smallest permutation of S such that T is not a subsequence of S′.

Example
input
7
abacaba
abc
cccba
acb
dbsic
bac
abracadabra
abc
dddddddddddd
cba
bbc
abc
ac
abc
output
aaaacbb
abccc
bcdis
aaaaacbbdrr
dddddddddddd
bbc
ac

Note
In the first test case, both aaaabbc and aaaabcb are lexicographically smaller than aaaacbb, but they contain abc as a subsequence.

In the second test case, abccc is the smallest permutation of cccba and does not contain acb as a subsequence.

In the third test case, bcdis is the smallest permutation of dbsic and does not contain bac as a subsequence.

问题链接CodeForces - 1617A Forbidden Subsequence
问题简述:给定2个字符串S和T,求S的字母最小排列,要求不能出现T中的排列顺序。
问题分析
 T=“abc”时,若S中同时包含字母abc,则需要特殊处理,按acb顺序输出。
 当 T=“abc” 且 S中包含字母a时,输出 a?c?b?⋯z?a?c?b?⋯z?,否则输出 a?b?c?⋯z?a?b?c?⋯z?,其中?表示前一字母重复次数。

AC的C++语言程序如下:

/* CodeForces - 1617A Forbidden Subsequence */

#include <stdio.h>
#include <string.h>

#define N 100 + 1
#define AL 26
char s[N], t[N];
int cnt[AL];

int main()

    int n;
    scanf("%d", &n);
    while (n--) 
        scanf("%s%s", s, t);

        memset(cnt, 0, sizeof cnt);
        for (int i = 0; s[i]; i++)
            cnt[s[i] - 'a']++;

        if (strcmp(t, "abc") == 0) 
            int cnt0 = cnt[0];
            for (int i = 0; i < AL; i++) 
                if (cnt0 && i == 1 && cnt[i] && cnt[i + 1])
                    while (cnt[i + 1]) 
                        putchar('a' + i + 1);
                        cnt[i + 1]--;
                    
                while (cnt[i]--) putchar('a' + i);
            
         else 
            for (int i = 0; i < AL; i++)
                while (cnt[i]--) putchar('a' + i);
        
        putchar('\\n');
    

    return 0;

以上是关于CodeForces - 1617A Forbidden Subsequence文本处理+计数排序的主要内容,如果未能解决你的问题,请参考以下文章

Dobbo问题及解决方案:forbid-consumer

com.alibaba.dubbo.rpc.RpcException: Forbid consumer 10.254.7.50 access service com.qingmu.core.servi

403重定向不起作用

c语言电脑上下载word时出现403forbiddwn怎么办

自定义错误页

gvrp的简单配置