您的位置:首页 > 其它

HihoCoder 1264 神奇字符串(暴力)

2016-02-14 22:41 351 查看
题意:

两个字符串A和B,|A|,|B|≤100,每个单位时间可以交换字符串A的相邻的两个字符

我们说两个字符串是非常相似的,当且仅当它们的编辑距离≤1

问最少需要多少时间,让A和B变得非常相似,保证存在一种这样的方案

分析:

首先看到100,显然O(n3)的大暴力

直接枚举跟B编辑是1的字符串,添加1个字母,删除1个字母,替换1个字母

然后根据冒泡排序模拟就好了(也就是计算逆序对的个数)

代码:

//
//  Created by TaoSama on 2016-02-14
//  Copyright (c) 2016 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#include <cassert>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int cc1[26], cc2[26];
string s, t;

int calc(string s, string t) {
if(s.size() != t.size()) return INF;
int ret = 0;
for(int i = 0; i < t.size(); ++i) {
char c = t[i];
string::size_type idx = s.find(c, i);
if(idx == string::npos) return INF;
for(int j = idx; j > i; --j) {
++ret;
swap(s[j], s[j - 1]);
}
}
assert(s == t);
return ret;
}

int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
//  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);

cin >> s >> t;

int ans = INF;
//add
for(int i = 0; i <= t.size(); ++i) {
for(char c = 'a'; c <= 'z'; ++c) {
string tmp = t;
tmp.insert(i, 1, c);
ans = min(ans, calc(s, tmp));
}
}
//del
for(int i = 0; i < t.size(); ++i) {
string tmp = t;
tmp.erase(i, 1);
ans = min(ans, calc(s, tmp));
}
//replace
for(int i = 0; i < t.size(); ++i) {
for(char c = 'a'; c <= 'z'; ++c) {
string tmp = t;
tmp[i] = c;
ans = min(ans, calc(s, tmp));
}
}
cout << ans << '\n';

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  暴力