思路:

暴力,模拟。

实现:

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std; const int INF = 0x3f3f3f3f; void str_swap(string & x, int i, int j)
{
char tmp = x[i];
x[i] = x[j];
x[j] = tmp;
} int cal(string x, string y)
{
int s = x.length();
int t = y.length();
if (s != t)
return INF;
int cnt = ;
for (int i = ; i < t; i++)
{
if (x[i] == y[i])
continue;
bool flag = false;
for (int j = i + ; j < s; j++)
{
if (x[j] == y[i])
{
for (int k = j; k > i; k--)
{
str_swap(x, k, k - );
cnt++;
}
flag = true;
break;
}
}
if (!flag)
{
return INF;
}
}
return cnt;
} int main()
{
string a, b;
cin >> a >> b;
int n = b.length();
int minn = INF;
for (int i = ; i <= n; i++)
{
for (char c = 'a'; c <= 'z'; c++)
{
string tmp = b;
tmp.insert(i, , c);
minn = min(minn, cal(a, tmp));
}
}
for (int i = ; i < n; i++)
{
for (char c = 'a'; c <= 'z'; c++)
{
string tmp = b;
tmp.erase(i, );
minn = min(minn, cal(a, tmp));
}
}
for (int i = ; i < n; i++)
{
for (char c = 'a'; c <= 'z'; c++)
{
string tmp = b;
tmp[i] = c;
minn = min(minn, cal(a, tmp));
}
}
cout << minn << endl;
return ;
}

总结:

学习了string 类 insert(可以在最后一个字符后面的位置插入字符或者字符串), erase方法。

hihocoder 神奇字符串的更多相关文章

  1. [LeetCode] Magical String 神奇字符串

    A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...

  2. [Swift]LeetCode481. 神奇字符串 | Magical String

    A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...

  3. Q481 神奇字符串

    神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几个元素如下:S = & ...

  4. Leetcode 481.神奇字符串

    神奇字符串 神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几个元素如下 ...

  5. Java实现 LeetCode 481 神奇字符串

    481. 神奇字符串 神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几 ...

  6. [题解]hihoCoder挑战赛18——题目1 神奇字符串

    题目地址:http://hihocoder.com/problemset/problem/1264 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 我们说两个字符串是非常 ...

  7. 【hihocoder 1039 字符串消除】模拟

    题目链接:http://hihocoder.com/problemset/problem/1039 题意:给定一个只由{A, B, C}组成的字符串s,长度为n, 故包含n+1个空隙:现要求在某个空隙 ...

  8. hihocoder 1485----hiho字符串

    hihocoder 1485:hiho字符串 描述 如果一个字符串恰好包含2个'h'.1个'i'和1个'o',我们就称这个字符串是hiho字符串. 例如"oihateher".&q ...

  9. Websocket协议的学习、调研和实现

    本文章同时发在 cpper.info. 1. websocket是什么 Websocket是html5提出的一个协议规范,参考rfc6455. websocket约定了一个通信的规范,通过一个握手的机 ...

随机推荐

  1. maven 项目 spring mvc + jdbc 配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. PHP出现Warning: A non-numeric value encountered问题的原因及解决方法

    本文介绍php出现Warning: A non-numeric value encountered问题,用实例分析出现这种错误的原因,并提供避免及解决问题的方法. <?php error_rep ...

  3. 连接sql2008时报错

    最近把公司的项目搭建到本地(周末回家要加班),可是连接后,发现程序后台出错,错误信息:不支持此服务器版本.目标服务器必须是 SQL Server 2000 或更高版本. 本地是SqlServer200 ...

  4. hashable

    Glossary — Python 3.6.5 documentation https://docs.python.org/3/glossary.html?highlight=equal hashab ...

  5. browser user agent

    乐视X501 UC浏览器1080x1920x32Mozilla/5.0 (Linux; U; Android 5.0.2; zh-CN; Letv X501 Build/DBXCNOP55013041 ...

  6. 实践001:char 类型字段在表中的长度

    Rainy on 20170215 1.同事在 写RFC的时候遇到报错:"YTST_001" 必须为扁平结构.不能将内部表.字符# 原因是自建结构中字段定义为了string 类型. ...

  7. Lightoj 1016 - Brush (II)

    After the long contest, Samee returned home and got angry after seeing his room dusty. Who likes to ...

  8. bacth参数说明 cmd parameter

    http://www.robvanderwoude.com/parameters.php Windows NT 4 introduced a set of new features for comma ...

  9. 一场由过滤器Filter引发的血案

    一场由过滤器Filter引发的血案 事件起因 本来应该是下图的登录界面 变成了这样 What's the fuck????? 抓狂 原因 解决方法: 在过滤器中给资源文件开个绿色通道

  10. Mac 下设置Android 环境变量 NDK

    1. 启动终端Terminal 2. 进入当前用户的home目录 输入cd ~ 3. 创建.bash_profile    输入touch .bash_profile4. 编辑.bash_profil ...