题意:
给定两个字符串,每次可以交换相邻两个字符,给出任意一组交换次数小于1e4的方案使得a串成为b串,输出交换的次数与位置,无解输出-1

n<=50

思路:每次找到第一个不相同的字符,从后面找到一个相同的换上来

若找不到相同的则输出-1

 #include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair const int N=;
int c[N];
char a[N],b[N];
int n; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
scanf("%d",&n);
scanf("%s",b+);
scanf("%s",a+);
int flag=;
for(int i=;i<=n;i++)
if(a[i]!=b[i]) {flag=; break;}
int ans=;
int noans=;
while(flag)
{
int k=;
flag=;
for(int i=;i<=n;i++)
if(a[i]!=b[i]){k=i; flag=; break;}
if(!k) break;
int f=;
for(int i=k+;i<=n;i++)
if(b[i]==a[k]){f=i; break;}
if((k)&&(!f)) {noans=; break;}
else
{
for(int j=f-;j>=k;j--)
{
c[++ans]=j;
char t=b[j+];
b[j+]=b[j];
b[j]=t;
}
}
}
if(noans) printf("-1");
else
{
printf("%d\n",ans);
for(int i=;i<=ans;i++) printf("%d ",c[i]);
}
return ;
}

【CF1015B】Obtaining the String(模拟)的更多相关文章

  1. B. Obtaining the String(模拟)

    比较水的模拟 思路:就是模拟题意 注意:把数组开大点,开始wa了几次就是这个原因 #include<iostream> #include<string> #include< ...

  2. STL string 模拟

    下面的代码来自c++ primer plus第5版第12章,书中代码写的非常好: // string1.h -- fixed and augmented string class definition ...

  3. Codeforces-B-Game with string(模拟栈)

    Two people are playing a game with a string ss, consisting of lowercase latin letters. On a player's ...

  4. zzulioj--1813--good string(模拟)

    1813: good string Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 93  Solved: 15 SubmitStatusWeb Boa ...

  5. HDOJ 5414 CRB and String 模拟

    CRB and String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  6. Codeforces Round #501 (Div. 3) B. Obtaining the String (思维,字符串)

    题意:有两个字符串\(S\)和\(T\),判断\(T\)是否能由\(S\)通过交换某位置的相邻字符得到,如果满足,输出交换次数及每次交换的位置,否则输出\(-1\). 题解:首先判断不满足的情况,只有 ...

  7. [agc007f] Shik and Copying String 模拟神题

    Description ​ "全"在十分愉快打工,第0天,给了他一个仅有小写字母构成的长度为N的字符串S0,在之后的第i天里,"全"的工作是将Si−1复制一份到 ...

  8. Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟)

    Vasya has a string s of length n. He decides to make the following modification to the string: Pick ...

  9. String源码

    /* * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...

随机推荐

  1. 使用xcode 8 调试ios10

    这几天更新了ios10,发现真机不能调试,弹出几个错,表示没有证书.用ios9的真机能调试, 真他么坑,总结一下解决方法. 在BuildSetting 的Signing中Code Signing Id ...

  2. c++ 定义一个结构体student,输入多个student的信息并以三种方式显示

    #include <iostream> #include <string> using namespace std; const int slen = 30; struct s ...

  3. NOIP模拟赛 机器人

    [题目描述] 早苗入手了最新的Gundam模型.最新款自然有着与以往不同的功能,那就是它能够自动行走,厉害吧. 早苗的新模型可以按照输入的命令进行移动,命令包括‘E’.‘S’.‘W’.‘N’四种,分别 ...

  4. NOIP模拟赛 经营与开发 小奇挖矿

    [题目描述] 4X概念体系,是指在PC战略游戏中一种相当普及和成熟的系统概念,得名自4个同样以“EX”为开头的英语单词. eXplore(探索) eXpand(拓张与发展) eXploit(经营与开发 ...

  5. linux常用命令(配置查看,定时任务)

    1.查看所有待挂载设备信息 fdisk -l # fdisk -l Disk /dev/sda: bytes heads, sectors/track, cylinders, total sector ...

  6. 【android】【android studio】修改emulator的本地化环境

    Changing the emulator locale from the adb shell To change the locale in the emulator by using the ad ...

  7. java上传附件,批量下载附件(一)

    上传附件代码:借助commons-fileupload-1.2.jar package com.str; import java.io.BufferedInputStream;import java. ...

  8. Python中的端口协议之基于UDP协议的通信传输

    UDP协议: 1.python中基于udp协议的客户端与服务端通信简单过程实现 2.udp协议的一些特点(与tcp协议的比较)        3.利用socketserver模块实现udp传输协议的并 ...

  9. pycharm-install scipy

    懒得装双系统,所以在win7下用pycharm,python2.7 虽然机子本身是64位,但是安装包的时候,我居然需要下载32位的??迷:) 这次装的是scipy.在pycharm里添加不了,根据网上 ...

  10. for_each_node(node)

    遍历各个pg_data_t节点. 1.定义在include/linux/nodemask.h中 /* * Bitmasks that are kept for all the nodes. */ en ...