题目如下:
AC代码如下:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
using namespace std;
char s1[30];
char s2[30];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
cin >> s1;
cin >> s2;
int a = strlen(s1);
int b = strlen(s2);
int ans = 0;
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
int cnt = 0;
int q = 0;
while(s1[i+q]==s2[j+q]&&i+q<a&&j+q<b)
{
cnt++;
ans = max(cnt,ans);
q++;
}
}
}
cout << a + b - 2 * ans << endl;
}
}
代码解析:
暴力求最长连续子串 的长度ans
两个原串的长度和 - 2*ans (a+b-2*ans)就是要删除的最少次数!
反思:
一开始想通过DP找到最长连续子串长度,但不太会就换思路了
然后想到暴力找,遗憾的是:我是仅通过双重for和新开的两个变量来维护下标,我居然没有想到用while(可能我傻了,狗头保命!!!),所以导致下标维护存在问题,没有通过所有测试数据。
最后的最后,
祝大家 新年快乐!!!
同时我也会砥砺前行,在新的一年大幅度提高自己的算法水平,不仅仅停留在板子题(再一次狗头保命)!!!