CF1506C Double-ended Strings(差不多就是找最长串问题)

简介: CF1506C Double-ended Strings(差不多就是找最长串问题)

题目描述



You are given the strings aa and bb , consisting of lowercase Latin letters. You can do any number of the following operations in any order:


  • if |a| > 0∣a∣>0 (the length of the string aa is greater than zero), delete the first character of the string aa , that is, replace aa with a_2 a_3 \ldots a_na2a3…an ;
  • if |a| > 0∣a∣>0 , delete the last character of the string aa , that is, replace aa with a_1 a_2 \ldots a_{n-1}a1a2…an−1 ;
  • if |b| > 0∣b∣>0 (the length of the string bb is greater than zero), delete the first character of the string bb , that is, replace bb with b_2 b_3 \ldots b_nb2b3…bn ;
  • if |b| > 0∣b∣>0 , delete the last character of the string bb , that is, replace bb with b_1 b_2 \ldots b_{n-1}b1b2…bn−1 .


Note that after each of the operations, the string aa or bb may become empty.


For example, if a=a= "hello" and b=b= "icpc", then you can apply the following sequence of operations:


  • delete the first character of the string aa \Rightarrow⇒ a=a= "ello" and b=b= "icpc";
  • delete the first character of the string bb \Rightarrow⇒ a=a= "ello" and b=b= "cpc";
  • delete the first character of the string bb \Rightarrow⇒ a=a= "ello" and b=b= "pc";
  • delete the last character of the string aa \Rightarrow⇒ a=a= "ell" and b=b= "pc";
  • delete the last character of the string bb \Rightarrow⇒ a=a= "ell" and b=b= "p".


For the given strings aa and bb , find the minimum number of operations for which you can make the strings aa and bb equal. Note that empty strings are also equal.


输入格式



The first line contains a single integer tt ( 1 \le t \le 1001≤t≤100 ). Then tt test cases follow.


The first line of each test case contains the string aa ( 1 \le |a| \le 201≤∣a∣≤20 ), consisting of lowercase Latin letters.


The second line of each test case contains the string bb ( 1 \le |b| \le 201≤∣b∣≤20 ), consisting of lowercase Latin letters.


输出格式



For each test case, output the minimum number of operations that can make the strings aa and bb equal.


题意翻译



你有两个字符串  a,b,你可以对这个字符串进行下面的操作:


  • 如果 a  非空,删除 a 的第一个字符。
  • 如果 a  非空,删除 a  的最后一个字符。
  • 如果 b  非空,删除  b 的第一个字符。
  • 如果 b  非空,删除 b  的最后一个字符。


求至少需要多少次操作使 a  和 b  完全相同。注意,我们认为两个空串也是完全相同的。


T  组数据。


输入输出样例



输入

5

a

a

abcd

bc

hello

codeforces

hello

helo

dhjakjsnasjhfksafasd

adjsnasjhfksvdafdser


输出  

0

2

13

3

20


题意分析;首先我们可以分析知道;我们要找一个二个串的公共最长串,然后我们把2个串的长度加起来减去公共最长串就是我们要的操作次数。具体操作看代码。


#include <bits/stdc++.h>
using namespace std;
void solve() 
{
  string a, b;
  cin >> a >> b;
  int maxk = 0;
  for (int i=0; i<a.size(); i++) 
  {
    for (int j=0; j<b.size(); j++) 
    {
      int k = 0;
      while (i+k < a.size() && j+k < b.size() && a[i+k] == b[j+k])k++;
      if (k > maxk) maxk = k;
    }
  }
  cout << a.size() + b.size() -2*maxk << endl;
}
int main() 
{
  int T;
  cin >> T;
  for (int i=1; i<=T; i++)solve();
}
相关文章
|
监控 前端开发
STM32F103标准外设库——RCC时钟(六)
STM32F103标准外设库——RCC时钟(六)
2391 0
STM32F103标准外设库——RCC时钟(六)
|
机器学习/深度学习 数据采集 算法
残差分析| 学习笔记
快速学习残差分析。
残差分析| 学习笔记
|
26天前
|
人工智能 缓存 自然语言处理
阿里云Token Plan最新优惠怎么领:额度上调70%以上、首购5折与夜间折扣规则详解‌
阿里云百炼 Token Plan 是百炼平台的 AI 模型订阅服务,以 Credits 统一计量抵扣模型调用与内置工具费用,分个人版与团队版两条产品线。当前三项条款并行生效:自2026年11月9日起个人版四档套餐额度提升约74%~78%(Lite、Essential、Standard 月费不变,Pro 额度上调、月费降至312元),存量用户自动补齐;新客首购5折,Lite 19.5元/月、Essential 39.5元/月,截至11月30日;夜间22:00至次日08:00指定模型享4折或5折。团队版按席位售卖,承诺不使用对话数据训练模型。
阿里云Token Plan最新优惠怎么领:额度上调70%以上、首购5折与夜间折扣规则详解‌
|
2月前
|
自然语言处理 JavaScript 前端开发
知更鸟 · relsent-bert-zh-large
知更鸟(relsent-bert-zh-large)是业界首个中文人际关系三维情感分析模型,可同步识别关系类型(7类)、情感极性(3类)与关系动态(亲近/稳定/疏远),平均F1达85.4%。ONNX轻量部署,支持多语言调用,开箱即用。
|
2月前
|
机器学习/深度学习 人工智能 自然语言处理
GEO优化从零开始:新手如何真正落地?
本文系统解析GEO(生成式引擎优化),澄清其非SEO替代而是延伸,强调“内容可信、结构清晰、索引扎实”三大根基。结合国内数据与实战案例,提供从认知校准、知识底座搭建到措辞优化的完整路径,并附四周落地节奏与可复用模板,助力内容团队零起点高效入场。
411 0
|
3月前
|
人工智能 缓存 监控
一文看懂 DeepSeek 调价回归:谷时省一半,你的 AI 成本账该这么算
9月10日中午12点起,DeepSeek Flash 系列执行新定价:空闲时段输入缓存命中 0.02 元/百万 token、缓存未命中 1 元、输出 4 元,高峰时段价格翻倍,谷时价格基本回到 8 月 17 日之前的老价。本文拆解这次调价背后的逻辑,把任务拆成实时交互、批量处理、增量缓存三类,说清哪类必须付高峰价、哪类挪到夜间就能省一半,以及如何用可复用的缓存前缀把输入成本压到几乎可忽略;最后聊聊价格回归背后,国产大模型从拼参数转向拼价格、拼体验的拐点。
|
缓存 PyTorch API
TensorRT-LLM 推理服务实战指南
`trtllm-serve` 是 TensorRT-LLM 官方推理服务工具,支持一键部署兼容 OpenAI API 的生产级服务,提供模型查询、文本与对话补全等接口,并兼容多模态及分布式部署,助力高效推理。
1436 155
|
4月前
|
人工智能 安全 Linux
DeployBrain AI - AI自动部署工具:说句话就能把项目部署上线
DeployBrainAI 是一款基于自然语言交互的智能部署工具,用户只需用中文描述需求(如"部署WordPress博客"或运行GitHub项目),AI即可自动完成环境检测、方案选择(Docker/源码等)、安装配置、服务启动及安全验证等全流程,支持Web项目、数据库、开源工具等多样化部署场景。 核心特点: 零基础友好:无需掌握Linux/Docker等技能,一句话触发自动化部署。
309 1
DeployBrain AI - AI自动部署工具:说句话就能把项目部署上线
|
4月前
|
人工智能 搜索推荐 数据安全/隐私保护
26年8月最新零基础如何两天自学GEO:一份可复制的自学路线图
我是小E,由数字出版CSM转型GEO优化。本文是零基础两天速成GEO的完整学习实录:涵盖概念原理、结构化改写、Schema标记、多平台适配及实操任务。全程免费、无需编程,适合职场人、创业者与转行者入门,助你成为AI时代GEO操盘手。
942 0
|
消息中间件 存储 负载均衡
【高可用】什么是异地多活、同城容灾?
异地多活与同城容灾均为提升系统高可用的分布式架构。前者实现跨地域数据中心实时同步与故障切换,保障全球服务连续性;后者聚焦同城内快速容灾,通过高速网络实现低延迟、高可靠的数据同步与负载均衡,适用于对延迟敏感的业务场景。
596 11