codeforces 1015B
1 second
256 megabytes
standard input
standard output
You are given two strings ss and tt. Both strings have length nn and consist of lowercase Latin letters. The characters in the strings are numbered from 11 to nn.
You can successively perform the following move any number of times (possibly, zero):
- swap any two adjacent (neighboring) characters of ss (i.e. for any i={1,2,…,n−1}i={1,2,…,n−1} you can swap sisi and si+1)si+1).
You can't apply a move to the string tt. The moves are applied to the string ss one after another.
Your task is to obtain the string tt from the string ss. Find any way to do it with at most 104104 such moves.
You do not have to minimize the number of moves, just find any sequence of moves of length 104104 or less to transform ss into tt.
The first line of the input contains one integer nn (1≤n≤501≤n≤50) — the length of strings ss and tt.
The second line of the input contains the string ss consisting of nn lowercase Latin letters.
The third line of the input contains the string tt consisting of nn lowercase Latin letters.
If it is impossible to obtain the string tt using moves, print "-1".
Otherwise in the first line print one integer kk — the number of moves to transform ss to tt. Note that kk must be an integer number between 00and 104104 inclusive.
In the second line print kk integers cjcj (1≤cj<n1≤cj<n), where cjcj means that on the jj-th move you swap characters scjscj and scj+1scj+1.
If you do not need to apply any moves, print a single integer 00 in the first line and either leave the second line empty or do not print it at all.
6
abcdef
abdfec
4
3 5 4 5
4
abcd
accd
-1
In the first example the string ss changes as follows: "abcdef" →→ "abdcef" →→ "abdcfe" →→ "abdfce" →→ "abdfec".
In the second example there is no way to transform the string ss into the string tt through any allowed moves.
题意:只能交换s1的相邻元素,为最少需要次可以将s1换成s2
题解:暴力就行,如果不相等,从当前位置一直找,找到相等的元素然后一个个换过来
代码如下
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e5+;
int a[];
int b[];
int ans[ maxn];
int main(){ int n;
char str1[];
char str2[];
cin>>n>>str1>>str2;
for(int i=;i<n;i++){
a[str1[i]-'a'+]++;
b[str2[i]-'a'+]++;
}
int flag=;
int m=;
for(int i=;i<=;i++){
if(a[i]!=b[i]){
flag=;
}
}
if(flag==){
puts("-1");
}else{
for(int i=;i<n;i++){
if(str1[i]==str2[i]){
continue;
}else{
for(int j=i; j<n; j++)
if(str1[j] == str2[i]){
for(int k=j-; k>=i; k--){
ans[m++]=k+;
swap(str1[k], str1[k+]);
}
break;
}
}
}
printf("%d\n",m);
for(int i=;i<m;i++){
printf("%d ",ans[i]);
}
puts("");
}
return ;
}
codeforces 1015B的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- phpcms2008网站漏洞如何修复 远程代码写入缓存漏洞利用
SINE安全公司在对phpcms2008网站代码进行安全检测与审计的时候发现该phpcms存在远程代码写入缓存文件的一个SQL注入漏洞,该phpcms漏洞危害较大,可以导致网站被黑,以及服务器遭受黑客 ...
- 2002: [Hnoi2010]Bounce 弹飞绵羊
2002: [Hnoi2010]Bounce 弹飞绵羊 https://www.lydsy.com/JudgeOnline/problem.php?id=2002 分析: 绵羊在弹飞的路径中相当于一棵 ...
- I/O Handler的管理(3)
另外连接地址:http://blogs.readthedocs.org/zh_CN/latest/Handler_mgr.html 本章目录 I/O Handler的管理 IO句柄与Select_Re ...
- 从webview中加载assets中的html文件
private void readHtmlFormAssets(){ WebSettings webSettings = tipsWebView.getSettings(); webSettings. ...
- localStorage简析
声明:引用自http://www.cnblogs.com/st-leslie/p/5617130.html 一.什么是localStorage.sessionStorage 在HTML5中,新加入了一 ...
- 第一篇 Python安装与环境变量的配置
开发语言有很多种,为什么选Python? 先对各种开发语言做个初识和分类如下:高级语言:Python Java.PHP C# Go ruby C++... ---> 字节码低级语言:C.汇编 - ...
- 维特比算法(Viterbi)及python实现样例
维特比算法(Viterbi) 维特比算法 维特比算法shiyizhong 动态规划算法用于最可能产生观测时间序列的-维特比路径-隐含状态序列,特别是在马尔可夫信息源上下文和隐马尔科夫模型中.术语“维特 ...
- MySQL查询所有库中表名
select table_name from information_schema.tables where table_schema='contract_ggpt' and table_type=' ...
- Java判断数字的奇偶
package anli; import java.util.Scanner; public class jiou { public static void main(String[] args){ ...
- 字面值常量&&转义序列
字面值常量举例: 字面值常量的分类 示例 备注 整型 42.024(八进制数).0x23(十六进制) short类型没有对应的字面值 浮点型 3.14.3.14E2(指数) 默认类型是double 字 ...