A. Message
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.

Examples
input
aaaaa
aaa
output
0
input
abcabc
bcd
output
1
input
abcdef
klmnopq
output
7
Note

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;
}

  

Codeforces 156 A——Message——————【思维题】的更多相关文章

  1. CF--思维练习-- CodeForces - 215C - Crosses(思维题)

    ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...

  2. Codeforces 675C Money Transfers 思维题

    原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...

  3. Codeforces 1090D - Similar Arrays - [思维题][构造题][2018-2019 Russia Open High School Programming Contest Problem D]

    题目链接:https://codeforces.com/contest/1090/problem/D Vasya had an array of n integers, each element of ...

  4. codeforces 1140D(区间dp/思维题)

    D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Codeforces 957 水位标记思维题

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  6. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  7. codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题

    http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...

  8. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  9. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. LoadRunner10个用户并发测试时分别取不同的参数运行脚本

    使用场景,比如说10个用户使用不同的账户名和密码同时并发登录,此时选择如下参数化策略: 参数选择:select next row 选择unique update value on 选择 once 另一 ...

  2. linux最基本命令

    1.cd命令 这是一个非常基本,也是大家经常需要使用的命令,它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径.如: cd /root/Docements # 切换到 ...

  3. Windows上编译OpenImageIO

    这里使用的是OpenImageIO 1.1.11,要将OIIO解压到[工作目录]/openimageio/oiio里面. 先要给OIIO打一个patch,patch文件是Blender提供的: 下载O ...

  4. 如何自动在Eclipse里对指定类或接口生成要覆盖的方法?

    我们经常遇到对对指定类或接口生成要覆盖的方法,但是我们不了解这些类和接口,完全手写不能保证完全正确,那么是否可以准确知道指定类或接口要被覆盖的方法,自动生成这些代码呢?是的,完全可以,按照下面步骤操作 ...

  5. maven+eclipse+ssm 环境搭建和启动

    该类工程环境搭建和启动方法 ------------------------------------------------------------------------------- 配置 jdk ...

  6. CiSCO 交换机配置 SSH 登陆

    CiSCO 交换机配置 SSH 登陆 题目:在三层交换机上仅运行 SSH 服务,且用户名和密码的方式登录交换机. (一)了解主机名与域名 ​ 1."主机名" 为该设备的名称 ​ 2 ...

  7. word的xml文件中空白页和换页

    ■ word中分页符(插入空白页): <w:r>                    <w:rPr>                    <w:rFonts w:as ...

  8. python常用模块之OS

    os模块偏于文件目录管理 <1>.常用方法 工作目录: os.getcwd() 返回当前工作目录 os.chdir(dir) 更改当前工作目录,相当于cd 目录文件操作: os.mkdir ...

  9. [转] iOS中@class #import #include 简介

    [转载自:http://blog.csdn.net/chengwuli125/article/details/9705315] 一.解析        很多刚开始学习iOS开发的同学可能在看别人的代码 ...

  10. sqlalchemy中使用event设置条件触发短信与邮件通知

    一.原因 近期在做短信与邮件通知系统.使用到了这一块.例如,当订单完成以后进行邮件短信的通知.虽然可以采用直接调用接口的方式实现,但有几个原因让我希望使用条件触发的方式 1.由于系统中支持线上线下以及 ...