Codeforces Round #501 (Div. 3) B Obtaining the String

翻译

给你两个字符串\(s\)与\(t\),你每次可以交换字符串\(s\)种相邻两个字符,请你输出字符串\(s\)变成\(t\)的步骤(如果输出\(k\),代表交换了\(k\)与\(k+1\)),如果有多组解,随意输出一种即可。

思路

这道题一开始考虑复杂了,导致我发奋图强到\(11:40\)才\(A\)掉,我\(12:00\)必须睡觉因为明天有课\(www\)。

实际不难,这题是\(SPJ\),我是这么想的:我们都知道任意\(1\)个字符可以通过交换相邻的两个字符来跑遍整个字符串。

进而可以得出:只要我们可以枚举字符串\(t\)的每一个字符,并让\(j\)从这个字符开始(这是为了方便后面的交换),找到字符串\(s\)中的第一个与他相等的字符,如果是合法的情况,那么每个字符都能对上去才对,所以找不到直接\(-1\);然后从与其一样的字符开始向其进军,一直到他为止,中间的步骤要记录下来。

然后,完事了,代码有注释。看来\(Div3\)对于我这种蒟蒻不算太水啊!

Code

#include<iostream>
#include<algorithm>
using namespace std;
int n,num,f,ans[1000001];
string s,t;
int main()
{
    cin>>n>>s>>t;
    for(int i=0;i<n;i++)
    {
        f=false;
        int j;
        for(j=i;j<n;j++)
            if(t[i]==s[j])
            {
                f=true;//找到第一个相等的并跳出,记录为找到
                break;
            }
        if(f==false)
            return cout<<-1<<endl,0;
        for(int k=j-1;k>=i;--k)
        {
            ans[++num]=k;
            swap(s[k],s[k+1]);
        }
    }
    cout<<num<<endl;
    for(int i=1; i<=num; i++)
        cout<<ans[i]+1<<" ";//因为我们是下标从0开始,所以要加一
    return 0;
}

原文地址:https://www.cnblogs.com/lyfoi/p/9485070.html

时间: 2024-08-30 06:22:04

Codeforces Round #501 (Div. 3) B Obtaining the String的相关文章

Codeforces Round #501 (Div. 3) F. Bracket Substring

题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60949 ....看不懂 设dp[i][j][l]表示前i位,左括号-右括号=j,匹配到l了 状态转移,枚举下一个要填的括号,用next数组求状态的l,分别转移 代码 #include<bits/stdc++.h> using namespace std; const int maxn = 207;

Codeforces Round #501 (Div. 3) A Points in Segments

翻译 现在有一个数轴,上面会有\(M\)个点,标号为\(1\)到\(N\),现在给你在数轴上的条\(N\)线段的起始与终止的点,问哪几个点没有被这样线段覆盖,从小到大输出. 思路 签到题目.感觉几乎和一道题一样:校门外的树,撞题是很尴尬.思路差不多,即为开一个数组,全部赋值为\(0\),输入的线段的时候,将其起点与终点的全部的点赋值为\(1\),最后跑一下看看那些为\(0\)的点就完事了. Code #include<iostream> using namespace std; int boo

Codeforces Round #501 (Div. 3) D Walking Between Houses

翻译 给你一条数轴(出题人很喜欢数轴啊),上面有排列着\(n\)个点,编号为\(1\)到\(n\)(你开始在\(1\)).每次你要从一个点移动到另一个点(可以重复).移动距离为两点坐标之差的绝对值,问你是否能否在\(k\)次机会内里一共移动\(s\)距离,并输出方案. 思路 第四题还是比较难的,赛后想了几分钟时才有头绪. 毫无疑问还是贪心(出题人很喜欢贪心啊)!那么我们分类讨论,先看不合法的情况 无论如何走你也走不到那个距离 无论如何走你也会走超那个距离 看来只要能走到那个距离除外都是不合法的,

Codeforces Round #354 (Div. 2) C. Vasya and String

题目大意:有一个(a|b)^N的由a和b构成的长度为N的字符串,允许修改其中k位.问能构成的最长的全为a或全为b的子串的长度最长为多少. 思路,用两个队列分别保存前K+1个a和b的位置,以i为结尾的最长的子串的长度就是i减去队列头元素的值. #include <iostream> #include <cstdio> #include <memory.h> #include <queue> using namespace std; int main(int a

Codeforces Round #297 (Div. 2)B Pasha and String

B. Pasha and String time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the

Codeforces Round #445 div.2 D. Restoration of string 乱搞

D. Restoration of string 题意:给你n个字符串,让你构造一个终串,使得这n个字符串都是终串的最小频繁子串,如果不存在输出NO.  最频繁子串:出现次数最多的子串 tags: 直接暴力怼?? #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a;

Codeforces Round #249 (Div. 2)-D

这场的c实在不想做,sad. D: 标记一下每一个点8个方向不经过黑点最多能到达多少个黑点. 由题意可知,三角形都是等腰三角形,那么我们就枚举三角形的顶点. 对于每一个定点,有8个方向可以放三角形. 然后枚举8个方向,然后枚举腰的长度.然后判断是否可行. #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>

Codeforces Round #360 (Div. 2) D 数学推导 E dp

Codeforces Round #360 (Div. 2) A  == B  水,但记一下: 第 n 个长度为偶数的回文数是  n+reverse(n). C    dfs 01染色,水 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i