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个字母
然后根据冒泡排序模拟就好了(也就是计算逆序对的个数)
代码:
两个字符串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; }
相关文章推荐
- 用fail2ban阻止SSH和VSFTP暴力破解密码
- 2015BJOI day1第三题 糖果candy
- 4495: Least Prime factor 找到最小质因子P的第N小正整数
- 暴力枚举法求最大连续和
- CodeForces 241F Race
- POJ 2965.The Pilots Brothers' refrigerator
- POJ 1753.Flip Game
- POJ 3295.Tautology
- hdu 5533 Dancing Stars on Me
- hdu5532Almost Sorted Array
- 没参加的2015百度之星——数矩形
- poj 3279 Fliptile
- Birthday Cake——直线分割
- Spiral——找规律暴力
- POJ 1166&IOI 1994 The blocks 暴力枚举 解题报告
- hdu 1248
- LCM Challenge(暴力)
- 小晴天老师系列——竖式乘法(暴力)
- URAL 1786 Sandro's Biography
- URAL 1788 On the Benefits of Umbrellas