CF1506C

题意就是我们通过几次删头或尾的操作,看看两个字符串能不能变成一样的

经过观察我们发现,因为我们只能去头去尾,所以本质上剩下的就是他们的公共子串

因为他问的是至少删除多少个数才能获得这个答案,所以反过来就是问,两个字符串的公共子串最长是多少

匹配好之后删除的个数就是其他的字符了,就是 l a − a n s − l b − a n s la - ans - lb - ans laanslbans

数据范围也不大,暴力统计一下就行(注:好好复习暴力算法

// Problem: C. Double-ended Strings
// Contest: Codeforces - Codeforces Round #710 (Div. 3)
// URL: https://codeforces.com/problemset/problem/1506/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Code by: ING__
// 
// Powered by CP Editor (https://cpeditor.org)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#define ll long long
#define re return

using namespace std;

typedef pair<int, int> PII;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

int T;

int main(){
	cin >> T;
	while(T--){
		char a[30];
		char b[30];
		scanf("%s", a + 1);
		scanf("%s", b + 1);
		
		int la = strlen(a + 1);
		int lb = strlen(b + 1);
		
		// 求一个最长在b中的子串的长度是多少
		
		int ans = 0;
		
		for(int x = 1; x <= la; x++){
			for(int y = 1; y <= lb; y++){
				if(a[x] == b[y]){
					int cnt = 1;
					for(int i = 1; i + x <= la && i + y <= lb; i++){
						if(a[x + i] == b[y + i]){
							cnt++;
						}
						else{
							break;
						}
					}
					ans = max(ans, cnt);
				}
			}
		}
		
		cout << la + lb - 2 * ans << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值