B:Obtaining the String

题目描述:
You are given two strings s and t. Both strings have length n and consist of lowercase Latin letters. The characters in the strings are numbered from 1 to n.

You can successively perform the following move any number of times (possibly, zero):

swap any two adjacent (neighboring) characters of s (i.e. for any i={1,2,…,n−1} you can swap si and si+1).
You can’t apply a move to the string t. The moves are applied to the string s one after another.

Your task is to obtain the string t from the string s. Find any way to do it with at most 104 such moves.

You do not have to minimize the number of moves, just find any sequence of moves of length 104 or less to transform s into t.

输入:
The first line of the input contains one integer n (1≤n≤50) — the length of strings s and t.

The second line of the input contains the string s consisting of n lowercase Latin letters.

The third line of the input contains the string t consisting of n lowercase Latin letters.

输出:
If it is impossible to obtain the string t using moves, print “-1”.

Otherwise in the first line print one integer k — the number of moves to transform s to t. Note that k must be an integer number between 0 and 104 inclusive.

In the second line print k integers cj (1≤cj<n), where cj means that on the j-th move you swap characters scj and scj+1.

If you do not need to apply any moves, print a single integer 0 in the first line and either leave the second line empty or do not print it at all.

提示:
In the first example the string s changes as follows: “abcdef” → “abdcef” → “abdcfe” → “abdfce” → “abdfec”.

In the second example there is no way to transform the string s into the string t through any allowed moves.

代码:

#include<bits/stdc++.h>
using namespace std;
#define N 100map<char, int> mp;   //通过map容器判断两字符串是否拥有相同数量的字母
vector<int> T;        //动态数组,存入每一步交换的操作
int n;
char a[N], b[N];int main()
{mp.clear();cin >> n;cin >> a >> b;/*利用关键字对其所映射的数值进行处理*/for(int i = 0; i < n; i ++){mp[a[i]] ++;mp[b[i]] --;}for(int i = 0; i < n; i ++){if(mp[a[i]]){          //当key对应的值不为0时说明a,b字符串每种字母数量不同,状态不合法cout << "-1" << endl;return 0;}}for(int i = 0; i < n; i ++){for(int j = i; j < n; j ++){if(b[i] == a[j]){     //因为要保持b不变,将a移换位置变成b,所以外层循环控制b,内层循环控制并改变a的顺序for(int k = j; k > i; k --){swap(a[k], a[k - 1]);T.push_back(k);       //将操作存入动态数组中}break;}}}cout << T.size() << endl;for(int i = 0; i < T.size(); i ++){printf("%s", i != 0 ? " " : ""); //使用三目运算符进行规范格式printf("%d",T[i]);}cout << endl;return 0;
}

题后总结:
1.可以用map容器判断两字符串是否除了顺序外都相同;
2.用动态数组存每一步的操作,vector< int > T;
T.push_back(x0); 将x0压入数组最后;
3.双层循环,外层控制模板,内层控制需要改变的字符串。

210413赛后总结相关推荐

  1. 第十二届河南省ACM大学生程序设计竞赛赛后总结

      这次是我跟菜瓜(色瓜)组织大家去比赛了,因为有天梯赛和CCPC省赛的组织经历,所以并没有出什么差错,比赛场地好评,这也算是头一个省赛在体育馆里面举办的了. 下面说一下这次比赛的题吧   先说总结: ...

  2. 智能视觉组赛博 -10赛后反馈

    中国计量大学赛博-10(智能视觉组)队员对于参加第十六届智能车竞赛进行总结与展望. §01 赛后反馈 一.实验室介绍 赛博智能车实验室成立于2012年,隶属于中国计量大学信息学院,历来以技术为核心,以 ...

  3. 2017 ACM/ICPC(西安)赛后总结

    早上8:00的高铁,所以不得不6点前起床,向火车站赶--到达西安后已经是中午,西工大距离西安北站大概3小时车程的距离,只好先解决午饭再赶路了--下午3.30的热身赛,一行人在3.35左右赶到了赛场,坐 ...

  4. 赛后总结:第四届工业大数据竞赛注塑成型

    赛后总结:第四届工业大数据竞赛注塑成型 原文首发于我的公众号 前言 以第四届工业大数据竞赛虚拟量测任务为例,介绍大家的思路.自己代码乱写,导致不知道最后要复现的是哪个,加上工作上各种人员优化,就没有进 ...

  5. ios开发 热搜词demo_一场比赛16个热搜,uzi因焕烽躺枪,阿bin评价赛后太揪心

    相信有关注S10电竞赛事的小伙伴都知道,就在10月31号晚上的S10总决赛中,DWG战队是以3:1比分击败了SN战队,成功夺得S10的全球总冠军,这次S10赛季算是真正的结束了! 不得不说,这次的S1 ...

  6. 2020 CCPC网络赛 赛后感

    第一次参加,做过去年19年网络赛的题,去年是四个水题稳做出,然后还有两个线段树和树状数组的题(好像是),所以本来对这次网络赛挺有信心的(去年好像四个题就能进,当然要手速快). 这次网络赛还是有四个水题 ...

  7. Codeforces Round #701 (Div. 2)赛后补题报告(A~D)

    Codeforces Round #701 (Div. 2)赛后补题报告(A~D) A. Add and Divide 原题信息 http://codeforces.com/contest/1485/ ...

  8. 百度机器阅读理解比赛赛后总结

    百度机器阅读理解比赛赛后总结 <!-- 文章内容 --><div data-note-content="" class="show-content&qu ...

  9. 4am永远 鼠标按键设置_4AM碾压性夺冠创PCL历史!韦神赛后采访彰显霸气

    <绝地求生>PCL秋季赛的战斗已经全部落下帷幕了,Team Razer 雷蛇战队成员,国内最具人气的4AM战队凭借着在季后赛的出色发挥,以316分的高分碾压全场,成功斩获本次PCL秋季赛的 ...

最新文章

  1. 浏览器开发人员工具使用初步图解
  2. ExtJs-GridPanel简单的增删改
  3. 高效的企业测试–测试框架(5/6)
  4. AI Challenger 2018:细粒度用户评论情感分析冠军思路总结
  5. 使用ModelArts自动学习完成猫狗声音分类
  6. 【clickhouse】clickhouse UTC 时间带有时区 如何写入
  7. 关于Tomcat+Nginx负载均衡与Jmeter服务器测压的日记
  8. php无需鉴权的接口,thinkphp5-restfulapi 博客 接口鉴权应用
  9. webpack 4.0 中 clean-webpack-plugin 的使用
  10. IDEA插件: 一键自动部署jar到远程服务器 使用 Cloud Toolkit 来部署应用到腾讯云、阿里云服务器
  11. hibernate数据类型之间的映射关系
  12. 网络安全之特洛伊木马的攻防战略(转)
  13. report machine实现报表分栏
  14. 符合Scorm的LMS系统
  15. android 获取图片的真实地址
  16. 苹果用Android发文,安卓和苹果怎么传文件 安卓和苹果传文件详细教程
  17. 蓝屏代码0x00000074
  18. Android设备与外接U盘实现数据读取操作
  19. fonts/fontawesome-webfont.woff2 404
  20. 力扣解题思路:488. 祖玛游戏

热门文章

  1. 简单的小程序——可操作的表白神器
  2. Python len()方法
  3. 计算机图形学:机器人的画法与填充
  4. 基于HyperLPR的车牌识别
  5. 从申请到调用:全国快递物流查询 API 使用教程
  6. 46個不可不知的常識
  7. 隐私保护和数据安全:区块链的隐私问题、零钞:基于zkSNARK的完美混币池、Hawk:保护合约数据私密性、Coco框架、Baby Zoe
  8. 我的Linux系统九阴真经
  9. Possible missing firmware /lib/firmware/rtl_nic/rtl8168fp-3.fw for module r8169
  10. 商淘软件多用户商城系统WSTMart v2.0.8程序发布