暑期训练赛(6)B
2014-08-05 18:19
204 查看
C. 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.
Sample test(s)
input
output
input
output
input
output
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.
//AC代码
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.
Sample test(s)
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.
//AC代码
/* 题意: 给出两个字符串,对上一个字符串做改变,使其变成下一个字符串 可以从两端删除增加字符也可以改变任一个字符,输出最少需要改变的次数。 */ #include<iostream> #include<queue> #include<cstdio> #include<algorithm> #include<cstring> #include<iomanip> #include<map> #include<cstdlib> #include<cmath> #include<vector> #define LL long long #define IT __int64 #define zero(x) fabs(x)<eps #define mm(a,b) memset(a,b,sizeof(a)) const int INF=0x7fffffff; const double inf=1e8; const double eps=1e-10; const double PI=acos(-1.0); const int Max=3001; using namespace std; char a[Max]; char b[Max]; int main() { scanf("%s",a); scanf("%s",b); int n = strlen(a); int m = strlen(b); int ans = n+m; //左边加若干个 for (int i=0; i<=m; i++) { int tmp = i; int k; for (k = 0; k<n && i+k<m; k++) { if (a[k] == b[i+k]) continue; else tmp++; } if (k+i<m) { tmp += (m-k-i); } if (tmp < ans) ans = tmp; } //左边切去若干个 for (int i=0; i<=n; i++) { int tmp = 0; int k; for (k=0; k<m && k+i<n; k++) { if (b[k] == a[i + k]) continue; else tmp++; } if (k < m) { tmp += (m - k); } if (tmp < ans) ans = tmp; } printf("%d\n",ans); return 0; }
相关文章推荐
- 2013 - ECJTU 暑期训练赛第三场-problem-D
- 2013 - ECJTU 暑期训练赛第三场-problem-L
- 2013 - ECJTU 暑期12级训练赛第一场-Problem-H
- 2013 - ECJTU 暑期训练赛第三场-problem-H
- 暑期训练赛(6)A题
- 2013 - ECJTU 暑期训练赛第四场-problem-A
- 暑期训练赛(6)C
- 2013 - ECJTU 暑期训练赛第四场-problem-C
- 2013 - ECJTU 暑期训练赛第八场-problem-A
- 暑期训练赛(6)D
- 暑期集训Day2训练赛D题Spreadsheets(codeforces 1B)
- 2013 - ECJTU 暑期训练赛第四场-problem-L
- 2013 - ECJTU 暑期训练赛第八场-problem-B
- 暑期训练赛(6)E
- 暑期集训训练赛2(uva11210) 你会打麻将吗?
- 2017暑期ACM俱乐部个人训练赛第2场 G题 Balanced Photo(树状数组求逆序对)
- 2017暑期ACM俱乐部个人训练赛第5场 G.礼物 (矩阵快速幂)
- 2013 - ECJTU 暑期训练赛第二场-problem-A
- 2013 - ECJTU 暑期训练赛第四场-problem-F
- 2013 - ECJTU 暑期训练赛第八场-problem-C