hihocoder 神奇字符串
思路:
暴力,模拟。
实现:
#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 神奇字符串的更多相关文章
- [LeetCode] Magical String 神奇字符串
A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...
- [Swift]LeetCode481. 神奇字符串 | Magical String
A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...
- Q481 神奇字符串
神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几个元素如下:S = & ...
- Leetcode 481.神奇字符串
神奇字符串 神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几个元素如下 ...
- Java实现 LeetCode 481 神奇字符串
481. 神奇字符串 神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几 ...
- [题解]hihoCoder挑战赛18——题目1 神奇字符串
题目地址:http://hihocoder.com/problemset/problem/1264 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 我们说两个字符串是非常 ...
- 【hihocoder 1039 字符串消除】模拟
题目链接:http://hihocoder.com/problemset/problem/1039 题意:给定一个只由{A, B, C}组成的字符串s,长度为n, 故包含n+1个空隙:现要求在某个空隙 ...
- hihocoder 1485----hiho字符串
hihocoder 1485:hiho字符串 描述 如果一个字符串恰好包含2个'h'.1个'i'和1个'o',我们就称这个字符串是hiho字符串. 例如"oihateher".&q ...
- Websocket协议的学习、调研和实现
本文章同时发在 cpper.info. 1. websocket是什么 Websocket是html5提出的一个协议规范,参考rfc6455. websocket约定了一个通信的规范,通过一个握手的机 ...
随机推荐
- 鸟哥的Linux私房菜-----12、学习使用Shell scripts
- Yii2 mongodb 扩展的where的条件增加大于 小于号
1. mongodb的where中有比較丰富的 条件.例如以下: static $builders = [ 'NOT' => 'buildNotCondition', 'AND' => ' ...
- 我的家乡:三河古镇已经登上央视CCTV-1新闻联播啦!
在烟雨朦胧时走在古镇的青石街上,别有一番风味!第一幅图为央视的直播车,第二副图为漂亮的三河夜景色!
- What to do about Eclipse's “No repository found containing: …” error messages?
As Mauro said: "you have to remove and re-add the Eclipse Project Update site, so that its meta ...
- MySQL迁移到SQLServer
手头有个Java老项目,数据库是基于MySQL的,我们要把它迁移到SQLServer2008. 采用微软的SSMA For MySQL:迁移助手Microsoft SQL Server Migrati ...
- SQLServer导出单表数据
采用生成脚本---仅数据.. 如果是部分数据,可以先把部分数据备份到一个表中 select * into .. from ...
- C项目实践--家庭财务管理系统
1.功能需求分析 家庭财务管理系统给家庭成员提供了一个管理家庭财务的平台,系统可以对家庭成员的收入和支出进行增加,删除.修改和查询等操作,并能统计总收入和总支出.其主要功能需求描述如下: (1)系统主 ...
- PHP 7.2 RC3 on CentOS/RHEL 7.3 via Yum
https://webtatic.com/packages/php72/ rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-lat ...
- (转载)synchronized代码块
原文:http://blog.csdn.net/luoweifu/article/details/46613015 作者:luoweifu 转载请标名出处 <编程思想之多线程与多进程(1)——以 ...
- BZOJ_3476_[Usaco2014 Mar]The Lazy Cow_扫描线+切比雪夫距离
BZOJ_3476_[Usaco2014 Mar]The Lazy Cow_扫描线+切比雪夫距离 Description It's a hot summer day, and Bessie the c ...