Delete from the Left
题目描述
You are given two strings s s s and t t t . In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by 1 1 1 . You can’t choose a string if it is empty.
For example:
- by applying a move to the string “where”, the result is the string “here”,
- by applying a move to the string “a”, the result is an empty string “”.
You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.
Write a program that finds the minimum number of moves to make two given strings s s s and t t t equal.
输入格式
The first line of the input contains s s s . In the second line of the input contains t t t . Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and 2 ⋅ 1 0 5 2\cdot10^5 2⋅105 , inclusive.
输出格式
Output the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings.
题面翻译
有两个字符串 s s s 和 t t t,每次操作可以删除一个字符串的第一个字符(如果该字符串为空串则不能删除),求让 s s s 和 t t t 相同的最小操作数。
样例 #1
样例输入 #1
test
west
样例输出 #1
2
样例 #2
样例输入 #2
codeforces
yes
样例输出 #2
9
样例 #3
样例输入 #3
test
yes
样例输出 #3
7
样例 #4
样例输入 #4
b
ab
样例输出 #4
1
提示
In the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to “est”.
In the second example, the move should be applied to the string “codeforces” 8 8 8 times. As a result, the string becomes “codeforces” → \to → “es”. The move should be applied to the string “yes” once. The result is the same string “yes” → \to → “es”.
In the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty.
In the fourth example, the first character of the second string should be deleted.
思路
双指针,创建两个指针
l
l
l和
r
r
r,从字符串的最后从头开始扫,并对比连续相同字符的数量。一旦出现不同的字符,扫描终止。
随后我们输出第一个字符串的长度与第二个字符串长度的和减去两倍的末尾相同字符数量。
则有:
(
l
e
n
a
+
l
e
n
b
)
−
2
×
k
(lena+lenb)−2×k
(lena+lenb)−2×k
代码
/*************************************************
Note:
*************************************************/
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <iomanip>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#define ll long long
#define ull unsigned long long
using namespace std;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
inline int read()
{
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
int main()
{
string a, b;
cin >> a >> b;
int lena = a.length();
int lenb = b.length();
int num = 0;
for (int i = lena - 1, j = lenb - 1; i >= 0, j >= 0; i--, j--)
{
if (a[i] == b[j])
num += 2;
else
break;
}
cout << lena + lenb - num << endl;
return 0;
}