原题链接:
解题思路:
经观察可知,S中子序列与T中连续子序列相同的最大长度为L,原序列长度减去L就是最少移动次数。
通过画面:
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <iostream>
using namespace std;
int counting(string S, string T);
int main()
{
string S, T;
cin >> S >> T;
printf("%d\n", counting(S, T));
return 0;
}
int counting(string S, string T)
{
int steps = 0;
int maxSubStr = 1;
for (int i = 2; i <= (int)S.length(); i++) {
bool flag = true;
for (int j = 0; j <= (int)S.length() - i && flag; j++) {
for (int k1 = 0, k2 = 0; k1 < (int)S.length() && k2 < i && flag; k1++) {
if (S[k1] == T[j + k2]) {
k2++;
}
if (k2 == i) {
flag = false;
}
}
}
if (!flag) {
maxSubStr = i;
}
else {
break;
}
}
steps = S.length() - maxSubStr;
return steps;
}