codeforce_157c_找两串字符串相同部分

32 篇文章 0 订阅
     Message

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.

题意:
给你串s,你可以对s的子串进行删除,左右端添加,改变任意字符,的操作,然后变为u串的最小操作;
解;
其实就是顺序找两串的相同部分
查找方式如下
这里写图片描述

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,u;
    while(cin>>s>>u)
    {
        int len1=s.length(),len2=u.length();
        int ans=(int)1e9,i,j,len=len2;
        for(i=0;i<len1;i++)
        {
            int k,cnt=0;
            for(k=0,j=i;k<len2&&j<len1;k++,j++)
            {
                if(u[k]==s[j])
                    cnt++;
            }
            ans=min(ans,len-cnt);
        }
        for(i=0;i<len2;i++)
        {
            int k,cnt=0;
            for(k=0,j=i;k<len1&&j<len2;k++,j++)
            {
                if(u[j]==s[k])
                    cnt++;
            }
            ans=min(ans,len-cnt);
        }
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值