HihoCoder 1264 神奇字符串(暴力)
来源:互联网 发布:吉祥兔源码下载 编辑:程序博客网 时间:2024/06/10 03:52
题意:
两个字符串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 << endlconst 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;}
0 0
- HihoCoder 1264 神奇字符串(暴力)
- hihocoder 1264 神奇字符串 (枚举+模拟)
- HihoCoder 1268 九宫(暴力)
- 搜索(字符串暴力)
- hihoCoder - 1094 - Lost in the City (暴力枚举!!)
- hihoCoder 1236 Scores 解题报告(bitset + 分段暴力)
- hihoCoder 1251 Today Is a Rainy Day(暴力)
- (暴力+各种算法)hihoCoder - 1353 满减优惠
- 字符串匹配(暴力匹配)
- KMP(字符串查找)(hihocoder)
- 【HihoCoder】Trie树 (字符串匹配)
- hihocoder Browser Caching(字符串hash)
- hihoCoder 1039 字符消除(字符串处理)
- hihoCoder 字符消除 (字符串处理)(枚举)
- 字符串暴力
- hihoCoder搜索二---骑士问题---暴力法
- Hihocoder 1054 滑动解锁 暴力dfs
- hihoCoder:1039字符串消除
- Atitit.ioc 动态配置文件guice 设计原理
- 常见排序算法
- ip
- 欢迎使用CSDN-markdown编辑器
- Java排序-快速排序
- HihoCoder 1264 神奇字符串(暴力)
- React Native控件之PullToRefreshViewAndroid下拉刷新组件讲解(20)
- Atitit.跨语言 java c#.net php js常用的codec encode算法api 兼容性 应该内置到语言里面
- SSH全套视频教程下载地址
- 【VB.Net】使用SHA256进行简单的XOR文本加密与解密
- 自定义表单(二)--拖拽(HTML版本)
- LeetCode - NimGame
- View的滑动冲突
- Android Service 完全解析