CodeForces 156A Message(暴力)
2016-05-07 07:50
288 查看
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
output
input
output
input
output
7
暴力
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
暴力
#include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> #include <math.h> #include <stdio.h> using namespace std; string a; string b; int main() { cin>>a>>b; int len1=a.length(); int len2=b.length(); int ans=10000000; int len=len2; if(len1<len2) { swap(a,b); swap(len1,len2); } for(int i=1;i<len2;i++) { int num=0; for(int k=i,j=0;k<len2&&j<len1;k++,j++) { if(b[k]==a[j]) num++; } ans=min(ans,len-num); } for(int i=0;i<len1;i++) { int num=0; for(int k=i,j=0;j<len2&&k<len1;j++,k++) { if(a[k]==b[j]) num++; } ans=min(ans,len-num); } printf("%d\n",ans); return 0; }
相关文章推荐
- 区域生长算法(附MATLAB代码实现)
- CodeForces 157B Trace
- CodeForces 157B Trace
- CodeForces 157A Game Outcome
- CodeForces 157A Game Outcome
- win7任务栏图标显示异常的解决方法
- 动态链接库DLL的加载:隐式加载(载入时加载)和显式加载(运行时加载)
- HDU 3578 Greedy Tino(双塔DP)
- 第一个DLL程序:动态链接库DLL教程,30分钟快速上手
- HDU 3578 Greedy Tino(双塔DP)
- POJ 2609 Ferry Loading(双塔DP)
- POJ 2609 Ferry Loading(双塔DP)
- 最小-最大计划报表-无法生成申请
- setContentView( )方法
- 数据科学家应该掌握的12种机器学习算法
- 吃了程序员这碗青春饭 40岁你该怎么办?
- setContentView( )方法
- 月入十万真的难吗?
- POJ 1015 Jury Compromise(双塔DP)
- POJ 1015 Jury Compromise(双塔DP)