PAT顶级 1015 Letter-moving Game (35分)

欢迎大家访问我的PAT TOP解题目录~

https://blog.csdn.net/qq_45228537/article/details/103671868

题目链接:

1015 Letter-moving Game (35分)

思路:

此题可以看作:删去第一个字符串的最少字符,使得剩下的字符串是第二个字符串的substring,求删去的最少字符数;
题意明白之后用O(n2)O(n^2)的暴力即可;

代码:

#include<bits/stdc++.h>

using namespace std;

int main(){
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif	
	string a, b;
	cin >> a >> b;
	int n = a.length(), ans = 0;
	for(int i = 0; i < n; i++){
		int p = i, q = 0;
		while(p < n){
			while(a[q] != b[p] && q < n) ++q;
			if(q == n) break;
			++p; ++q;
		}
		ans = max(ans, p - i);  //   cerr << p << ' ' << i << '
';
	}
	cout << n - ans;
	return 0;
}
原文地址:https://www.cnblogs.com/yuhan-blog/p/12308697.html