C. Double-ended Strings
You are given the strings a and b, consisting of lowercase Latin letters. You can do any number of the following operations in any order:
if |a|>0 (the length of the string a is greater than zero), delete the first character of the string a, that is, replace a with a2a3…an;
if |a|>0, delete the last character of the string a, that is, replace a with a1a2…an−1;
if |b|>0 (the length of the string b is greater than zero), delete the first character of the string b, that is, replace b with b2b3…bn;
if |b|>0, delete the last character of the string b, that is, replace b with b1b2…bn−1.
Note that after each of the operations, the string a or b may become empty.
For example, if a=“hello” and b=“icpc”, then you can apply the following sequence of operations:
delete the first character of the string a ⇒ a=“ello” and b=“icpc”;
delete the first character of the string b ⇒ a=“ello” and b=“cpc”;
delete the first character of the string b ⇒ a=“ello” and b=“pc”;
delete the last character of the string a ⇒ a=“ell” and b=“pc”;
delete the last character of the string b ⇒ a=“ell” and b=“p”.
For the given strings a and b, find the minimum number of operations for which you can make the strings a and b equal. Note that empty strings are also equal.
Input
The first line contains a single integer t (1≤t≤100). Then t test cases follow.
The first line of each test case contains the string a (1≤|a|≤20), consisting of lowercase Latin letters.
The second line of each test case contains the string b (1≤|b|≤20), consisting of lowercase Latin letters.
Output
For each test case, output the minimum number of operations that can make the strings a and b equal.
Example
Input
5
a
a
abcd
bc
hello
codeforces
hello
helo
dhjakjsnasjhfksafasd
adjsnasjhfksvdafdser
Output
0
2
13
3
20
题意
通过删减两个字符串的首或尾字符,使两个字符串相等。
题解
有题意可以得到,只删除首尾字符使两个字符串相等,那么我们可以等价于找到两个字符串中最长的公共子字符串。
这个时候我们可以通过一个二维的数组来找到这个最长子字符串
标记相同的字符所在的二维坐标为一,
比如
abcde 和bcdea
a b c d e
b 0 1 0 0 0
c 0 0 1 0 0
d 0 0 0 1 0
e 0 0 0 0 1
a 1 0 0 0 0
既然这样,那我们是不是可以通过将将要标记的字符标记为上一个标记的字符加一来确定一个字符串的长度,也就变为了
a b c d e
b 0 1 0 0 0
c 0 0 2 0 0
d 0 0 0 3 0
e 0 0 0 0 4
a 1 0 0 0 0
由此我们就可以得到最大的子字符串的长度,再用两个字符串的长度减去这给最大子字符串的两倍,就得到了结果。
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<memory>
#include<map>
#include<iomanip>
using namespace std;
#define ll long long
int c[25][25] = { 0 };
ll st(string a, string b) {
ll max = 0;
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b.size(); j++) {
if (a[i] == b[j]) {
if (i > 0 && j > 0) {
c[i][j] = c[i - 1][j - 1] + 1;
}
else c[i][j] = 1;
}
if (c[i][j] > max)max = c[i][j];
}
}
return max;
}
int main() {
int t;
cin >> t;
while (t--) {
string s;
string str;
cin >> s >> str;
ll sum = st(s, str);
for (int i = 0; i < s.size(); i++) {
for (int j = 0; j < str.size(); j++) {
c[i][j] = 0;
}
}
cout << (s.size() + str.size() - 2 * sum) << endl;
}
return 0;
}
该博客探讨了一种字符串操作问题,其中目标是通过删除字符串的首或尾字符,使得两个给定的字符串相等。问题转化为寻找两个字符串的最长公共子串。文章介绍了一种使用二维数组动态规划的方法来计算最长公共子串的长度,并给出了C++代码实现。最终,通过计算字符串总长度减去两倍最长公共子串长度得到所需操作次数。
341

被折叠的 条评论
为什么被折叠?



