天天看点

Codeforces Round #579 (Div. 3) - D1. Remove the Substring (easy version)

**D1. Remove the Substring (easy version)

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

The only difference between easy and hard versions is the length of the string.**

You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).

For example, the strings “test”, “tst”, “tt”, “et” and “” are subsequences of the string “test”. But the strings “tset”, “se”, “contest” are not subsequences of the string “test”.

You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.

If you want to remove the substring s[l;r] then the string s will be transformed to s1s2…sl−1sr+1sr+2…s|s|−1s|s| (where |s| is the length of s).

Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.

Input

The first line of the input contains one string s consisting of at least 1 and at most 200 lowercase Latin letters.

The second line of the input contains one string t consisting of at least 1 and at most 200 lowercase Latin letters.

It is guaranteed that t is a subsequence of s.

Output

Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.

Example

inputCopy

bbaba

bb

outputCopy

3

inputCopy

baaba

ab

outputCopy

2

inputCopy

abcde

abcde

outputCopy

inputCopy

asdfasdf

fasd

outputCopy

3

题意: 删除S串中的某一区间[L,R],使得T串仍然是S串的子串,求删除的区间的最大的长度.

做法:贪心+暴力.从S串的最左边向右遍历 对比x = [0,lent]个 T的字母,那么相应的就从S串的最右边向左遍历 对比 lent - x 个T的字母;然后计算max区间就ok了

AC代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
 
string s,t;
 
int main() {
	ios::sync_with_stdio(false);
	while(cin >> s >> t) {
		int lens = s.length(),lent = t.length();
		int maxx = -1;
		for(int p = 0; p <= lent; p++) {          //枚举t串的前p个; 
			int j = 0,sum = 0,k,i;
			bool isin1 = false,isin2 = false;
			for(i = 0; i < lens-p-1,j < p; i++) {
				if(s[i] == t[j]) {
					j++;
					sum = 0;
				} else {
					sum++;
				}
				isin1 = true;
				maxx = max(sum,maxx);
			}
			
			j = 0;
			sum = 0;
			for(k = lens-1; k >= 0+lent-p-1,j < lent-p; k--) { //枚举t串的lent - p个
				if(t[lent-1-j] == s[k]) {
					j++;
					sum = 0;
				} else {
					sum++;
				}
				isin2 = true;
				maxx = max(sum,maxx);
			}
			//if(p == lent || p == lent-1){
				//cout <<p << " "<< maxx << " " << k << " " << i << endl;
			//}
			if(isin2 == true && isin1 == true){
				maxx = max(maxx,(k+1)-(i-1)-1);
			}else if(isin2 == true && isin1 == false){
				maxx = max(maxx,k+1-i);
			}else if(isin2 == false && isin1 == true){
				maxx = max(maxx,k-(i-1));
			}
		}
		cout << maxx << endl;
	}
}