AT3741 String Problem 题解

文章讨论了如何通过动态规划方法判断两个字符串s和t是否可以通过插入和删除A、B字符操作变为相同。通过构建动态规划数组f来表示两个子串的匹配状态,最终检查f数组的对应元素是否为真。

题意

给定两个字符串 sssttt,可以对 sss 字符串进行如下的两种操作:

  • 对字符串 sss 的任意位置删去 AAA 字符。

  • 对字符串 sss 的任意位置插入 BBB 字符。

问是否可以进行若干次操作后将 sss 变为 ttt

解决

考虑动态规划做法,时间复杂度 O(ls×lt)O(l_s \times l_t)O(ls×lt)

一个动态规划数组 ffffi,j\mathit{f}_{i,j}fi,j 表示 sis_isitjt_jtj 是否能匹配。

动态规划分成两个阶段解决。

  1. 初始化:

f0,0=1\mathit{f}_{0,0}=1f0,0=1(这个前往别忘)

{fi,0=fi,0∣fi−1,0si=Af0,i=f0,i∣f0,i−1ti=B\begin{cases}\mathit{f}_{i,0}=\mathit{f}_{i,0}|\mathit{f}_{i-1,0}&s_i=A\\\mathit{f}_{0,i}=\mathit{f}_{0,i}|\mathit{f}_{0,i-1}&t_i=B\end{cases}{fi,0=fi,0fi1,0f0,i=f0,if0,i1si=Ati=B

  1. 动态规划:

fi,j={fi,j∣fi−1,jsi=Afi,l∣fi,l−1ti=Bfi,j∣fi−1,j−1si=tj\mathit{f}_{i,j}=\begin{cases}\mathit{f}_{i,j}|\mathit{f}_{i-1,j}&s_i=A\\\mathit{f}_{i,l}|\mathit{f}_{i,l-1}&t_i=B\\\mathit{f}_{i,j}|\mathit{f}_{i-1,j-1}&s_i=t_j\end{cases}fi,j=fi,jfi1,jfi,lfi,l1fi,jfi1,j1si=Ati=Bsi=tj

最后判断 fls,lt\mathit{f}_{l_s,l_t}fls,lt 是否为真即可。

代码

#include<bits/stdc++.h>
using namespace std;
string s,t;
bool f[1010][1010];
int main()
{
    cin>>s>>t,f[0][0]=1,s='0'+s,t='0'+t;
    for(int i=1;i<s.size();i++) if(s[i]=='A') f[i][0]=f[i][0]|f[i-1][0];
    for(int i=1;i<t.size();i++) if(t[i]=='B') f[0][i]=f[0][i]|f[0][i-1];
    for(int i=1;i<s.size();i++)
    {
        for(int j=1;j<t.size();j++)
        {
            if(s[i]=='A') f[i][j]=f[i][j]|f[i-1][j];
            if(t[j]=='B') f[i][j]=f[i][j]|f[i][j-1];
            if(s[i]==t[j]) f[i][j]=f[i][j]|f[i-1][j-1];
        }
    }
    if(f[s.size()-1][t.size()-1]) cout<<"YES";
    else cout<<"NO";
    return 0;
}
A-1 Intersection Set of Prime Factors(c++题解,禁止有注释,请严格按找题目要求,输出末尾没有空格) 分数 20 作者 陈越 单位 浙江大学 Given a positive integer n. Select two distinct digits from the decimal repersentation(十进制表示)of n, we obtain another integer m. What is the size of the intersection set(交集)of the prime factors of n and m? For example, given n=623457198, its prime factor set is A = {2, 3, 7, 13, 380621}. Swapping 2 and 9 gives us m=693457128, of which the prime factor set is B = {2, 3, 7, 13, 109, 971}. Then the intersection set of A and B is {2, 3, 7, 13}, with 4 factors. Input Specification: Each input file contains one test case, which gives a positive integer n (10<n≤10 9 ). It is guaranteed that there are at least 2 distinct digits in n. Output Specification: Swap any pair of digits in n to obtain m, you are supposed to find the m with the largest intersection set of the prime factors of n and m. Output in a line the number of the prime factors in the intersection set, together with m. The numbers must be separated by 1 space, and there must be no extra space at the beginning or the end of the line. In case such an m is not unique, output the one with the smallest value. Sample Input: 623457198 Sample Output: 4 123457698 Hint: There are two m's with 4 common factors. Besides the one given in the problem description, we can also swap 6 and 1 to obtain 123457698. This number has a prime factor set {2, 3, 7, 13, 23, 29, 113}, and so the intersection set is also {2, 3, 7, 13}. This number is in the ouput because it is smaller than 693457128. 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB
08-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值