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;
}
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;
}
相关文章推荐
- 1003. Universal Travel Sites (35)解题报告
- 1008. Airline Routes (35)解题报告
- RQNOJ 35 营救 解题报告
- 1006. Tree Traversals - Hard Version (35)解题报告
- pat-top 1015. Letter-moving Game (35)
- 剑指Offer 面试题35:第一个只出现一次的字符 解题报告(华为OJ034-找出字符串中第一个只出现一次的字符)
- PAT TOP 1015. Letter-moving Game (35)
- 1011. Cut Rectangles (35)解题报告
- 1012. Greedy Snake (35)解题报告
- PAT (Top Level) Practise 1015. Letter-moving Game (35)
- 1010. Lehmer Code (35)解题报告
- pat 1015. Letter-moving Game (35)(LCS)
- 1013. Image Segmentation (35)解题报告
- 1014. Circles of Friends (35)解题报告
- 1005. Programming Pattern (35)解题报告
- 1016. Uniqueness of MST (35)解题报告
- 1015. Letter-moving Game (35)
- 1017. The Best Peak Shape (35)解题报告
- PAT 1015. Letter-moving Game (35)
- 1002. Business (35)解题报告