您的位置:首页 > 其它

1015. Letter-moving Game (35)解题报告

2017-06-18 12:41 302 查看
原题链接:

1015. Letter-moving Game (35)

解题思路:

经观察可知,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: