跳过正文
  1. Posts/

codeforces 501 B. Obtaining the String

·468 字·1 分钟

题目链接:http://codeforces.com/contest/1015/problem/B

题意: 给出字符串s和字符串t,问一个将s变为t的策略。 可以做的变换为,交换s中相邻的字符串,该操作最多不能超过4000次,字符串长度最大为50.

思路:

首先可以确定,当两个字符串的组成相同时(也就是有同样的字符组成,只是位置可能有所不同)一定有解。

考虑最坏情况,每个字符都要交换到最远的地方,总的操作数<50*50<4000,因此一定有解。

判断组成是否相同可以用multiset

代码实现
 1/* ***********************************************
 2Author :111qqz
 3mail: renkuanze@sensetime.com
 4Created Time :2018年10月03日 星期三 16时11分29秒
 5File Name :b.cpp
 6************************************************ */
 7#include <bits/stdc++.h>
 8#define ms(a,x) memset(a,x,sizeof(a))
 9typedef long long LL;
10#define pi pair < int ,int >
11#define MP make_pair
12using namespace std;
13const double eps = 1E-8;
14const int dx4[4]={1,0,0,-1};
15const int dy4[4]={0,-1,1,0};
16const int inf = 0x3f3f3f3f;
17int n;
18string s,t;
19multiset<char>A,B;
20int main()
21{
22        #ifndef  ONLINE_JUDGE
23        freopen("./in.txt","r",stdin);
24  #endif
25  cin>>n;
26  cin>>s>>t;
27  for ( auto x : s) A.insert(x);
28  for (auto x: t) B.insert(x);
29  if (A!=B)
30  {
31    puts("-1");
32    return 0;
33  }
34  vector<int>ans;
35  //可以保证处理过的一定相等
36  for ( int i = 0 ; i < n ; i++)
37  {
38    if (s[i]==t[i]) continue;
39    string sub_str = s.substr(i,50);
40    // cout<<"sub_str:"<<sub_str<<endl;
41    int pos = sub_str.find(t[i])+i;
42    //  cout<<"pos:"<<pos<<endl;
43    for ( int j = pos; j  >= i+1 ; j-- )
44    {
45      ans.push_back(j);
46      swap(s[j-1],s[j]);
47      //  cout<<s<<endl;
48    }
49  }
50  cout<<ans.size()<<endl;
51  for ( auto x : ans) cout<<x<<" ";
52
53  #ifndef ONLINE_JUDGE
54  fclose(stdin);
55  #endif
56    return 0;
57}

相关文章

Codeforces eductional round 29

·2705 字·6 分钟
比赛链接 10个月没写题了,菜啊。进行一点恢复性训练好了。 A: 给一个数,可以在填写若干(或者0)个前缀0,问能否变成回文数。

codeforces #381 div2

·2406 字·5 分钟
http://codeforces.com/contest/740 A:现在有n个某种物品,要买k个使得n+k是4的倍数,可以的购买方案为a元1个,b元2个,c元3个,每种方案都可以买无限多。

codeforces #346 div 2 A. Round House

·160 字·1 分钟
题目链接 水题 乱搞。 1/* *********************************************** 2Author :111qqz 3Created Time :2016年03月30日 星期三 23时59分47秒 4File Name :code/cf/#346/A.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33int n,a,b; 34int main() 35{ 36 #ifndef ONLINE_JUDGE 37 freopen("code/in.txt","r",stdin); 38 #endif 39 cin>>n>>a>>b; 40 a = a + b; 41 while (a<=0) a+=n; 42 while (a>n) a-=n; 43 cout<<a<<endl; 44 45 #ifndef ONLINE_JUDGE 46 fclose(stdin); 47 #endif 48 return 0; 49}

codeforces croc 2016 C. Enduring Exodus

·475 字·1 分钟
题目链接 题意:给出n和k,给出一个长度为n的字符串表示房间的占用情况(0表示没占用,1表示已占用),从n个房间中找出k+1个,使得k+1中的k个距离k+1个中的1个的距离和最小。

codeforces 519 C. A and B and Team Training

·271 字·1 分钟
http://codeforces.com/problemset/problem/519/C 题意:两种组队方式,3人一组,1个大牛+2个蒟蒻或者1个蒟蒻+2个大牛。给定大牛和蒟蒻的个数。问最多能组多少队。 思路:线性规划。设两种队分别有x,y个即可。 突然发现这题以前做过。。。比当时的代码简单了一些。还不错。