Codeforces 156 A——Message——————【思维题】
2 seconds
256 megabytes
standard input
standard output
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
- Insert one letter to any end of the string.
- Delete one letter from any end of the string.
- Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
aaaaa
aaa
0
abcabc
bcd
1
abcdef
klmnopq
7
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message.
题目大意:给你两个串a,b,只能在尾部增删字符,可以在任意位置改变字符,问你最少需要多少次改变,让a中的字串变为b。
解题思路:直接找到最长可以匹配的字符个数,然后最后用len2-ans就是最少需要改动的次数。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
#pragma comment(linker, "/STACK:102400000,102400000")
const int maxn = 1e3 + 300;
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ULL;
char s1[maxn*2],s2[maxn*2];
int dp[maxn*2][maxn*2];
int main(){
while(scanf("%s%s",s1+1,s2+1)!=EOF){
int len1, len2;
len1 = strlen(s1+1);
len2 = strlen(s2+1);
int ans = 0;
for(int i = 1; i <= len1; i++){
for(int j = 1; j <= len2; j++){
ans = max(ans, dp[i][j] = dp[i-1][j-1] + (s1[i] == s2[j]) );
}
}
//for(int i = 1; i <= len1; i++){
// for(int j = 1; j <= len2; j++){
// printf("%d ",dp[i][j]);
// }puts("");
// } cout<<len2 - ans<<endl;
}
return 0;
}
最新文章
- C语言 &#183; Anagrams问题
- 一天一小段js代码(no.3)
- plsql客户端显示菜单等
- Failed to execute goal on project MakeFriends: Could not resolve dependencie The POM for .chengpai.jtd:jtd-service-api:jar:1.0-SNAPSHOT is missing, no dependency information available
- mysql 数据类型,字符集
- django 用户登陆注册
- factory工厂模式之简单工厂SimpleFactory
- FROM_UNIXTIME 格式化MYSQL时间戳函数
- JavaScripts+jquery
- try{} catch(…){} 讨论(转)
- wait与sellp方法区别
- 【转载】IdentityServer4 使用OpenID Connect添加用户身份验证
- 详细介绍关联规则Apriori算法及实现
- 第十三届东北师范大学程序设计竞赛热身赛 C(exgcd+欧拉函数)
- React Fiber 数据结构揭秘
- c/c++ 图的创建及图的相关函数(链表法)
- 简单规划dp sumsets
- JS 变量是否有值的判断
- 再有人问你volatile是什么,把这篇文章也发给他
- Caffe 学习:Eltwise层