当前位置:网站首页>CodeChef STRMRG String Merging (dp)
CodeChef STRMRG String Merging (dp)
2022-08-10 10:20:00 【51CTO】
Description
All submissions for this problem are available.
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
For a string S, let’s define a function F(S) as the minimum number of blocks consisting of consecutive identical characters in S. In other words, F(S) is equal to 1 plus the number of valid indices i such that Si ≠ Si+1.
You are given two strings A and B with lengths N and M respectively. You should merge these two strings into one string C with length N+M. Specifically, each character of C should come either from A or B; all characters from A should be in the same relative order in C as in A and all characters from B should be in the same relative order in C as in B.
Compute the minimum possible value of F(C).
Input
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains two space-separated integers N and M.
The second line contains a single string A with length N.
The third line contains a single string B with length M.
Output
For each test case, print a single line containing one integer — the minimum possible value of F(C).
Constraints
1 ≤ T ≤ 100
1 ≤ N, M ≤ 5,000
1 ≤ sum of N in all test cases ≤ 5,000
1 ≤ sum of M in all test cases ≤ 5,000
strings A, B consist only of lowercase English letters
Example Input
Example Output
题意
对于字符串 S,定义函数 F(S) 为:最少可以将 S 划分为几个连续的子串,使得每个子串仅包含相同的字符。换句话说,F(S) 等于 1 加上满足 Si ≠ Si+1 的合法下标 i 的数量。
给定两个字符串 A 和 B,长度分别为 N 和 M。你需要将这两个字符串合并成一个长度为 N + M 的字符串 C。C 的每个字符要么来源于 A,要么来源于 B,且来源于 A 的字符的相对顺序应当与在 A 中一致,来源于 B 的字符亦然。
请求出 F(C) 最小可能的值。
思路
unique 以后直接做 LCS 即可。
AC 代码
边栏推荐
猜你喜欢
Redis(三)——配置文件详解、发布和订阅、新数据类型
bus事件总线 使用
阻塞队列与线程池原理
[Concept of Theory of Knowledge] "Progress in the Theory of Reason" University of Leuven 2022 latest 220-page doctoral dissertation
14道高频手写JS面试题及答案,巩固你的JS基础
CatchAdmin实战教程(四)Table组件之自定义基础页面
ECCV 2022 | 视频理解新框架X-CLIP:仅用微调的成本,达到预训练的全能
「敏捷建模」纪律:敏捷设计理念
The web project accesses static resources inside the reference jar
LCD DRM驱动框架分析二
随机推荐
ESP8266-Arduino编程实例-MQ-8氢气传感器驱动
Tencent releases the second-generation version of the quadruped robot Max, which completes jumps and somersaults on the plum blossom pile
【Redis】Redis入门教程(介绍 下载安装 Jedis 图形化界面)
「第二部:容器和微服务架构」(1) 基于容器应用架构设计原则
2022.8.9-----leetcode.1413
ZZULIOJ 1116 Delete elements [delete]
LeetCode Algorithm 914. 卡牌分组
Array of shell scripts
LeetCode Algorithm 1472. 设计浏览器历史记录
chart.js水平柱状图插件
中国驻越南使馆提醒在越北部、中部地区中国公民做好台风“木兰”安全防范
Text selection rounded style border-radius
MySQL 执行查询流程
LeetCode Algorithm 1721. 交换链表中的节点
ESP8266 Tutorial 2 - Burn AT Firmware
在兄弟连战狼班参加PHP培训做行业领先人才
【FAQ】【Push Kit】 华为怎么设置角标
14道高频手写JS面试题及答案,巩固你的JS基础
[Concept of Theory of Knowledge] "Progress in the Theory of Reason" University of Leuven 2022 latest 220-page doctoral dissertation
Redis(六)——Redis6的事务和锁机制(未完成,待补)