zoukankan      html  css  js  c++  java
  • 字符串拼接

    题目:给定3个字符串a, b, c。你的任务是判断c是否可由a, b拼接出来。

    c可由a, b拼接则意味着存在一种情况,将c拆分成两个子字符串,这两个子字符串分为等于a, b。注,c在拆分过程中,c中的每个字符只能属于两个子串中的一个。

    输入包含多组样例,样例数不超过20。

    第一行一个整数T,表示样例数。

    接下来3T行,每三行为一组样例。每组样例包含3行,分别为字符串a, b, c(12000)。

     对于每组输入,输出Yes,如果c可由a, b拼接; No,如果不可拼接。

    样例输入:

    2
    abc
    def
    adebcf
    abc
    def
    abecdf
    样例输出:
    Yes
    No
    请注意,字符之间的顺序不能改变。
    思路:dp[i][j]:表示第 1 个字符串的第 i 个字符,第 2 个字符串的第 j 个位置
    状态转移:dp[i][j]=dp[i][j]||dp[i-1][j];
    dp[i][j]=dp[i][j]||dp[i][j-1];
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<stack>
     7 #include <bitset>
     8 #include<set>
     9 #include<map>
    10 #include<vector>
    11 #include<cmath>
    12 #include<string>
    13 typedef long long ll;
    14 using namespace std;
    15 #define maxn 2005
    16 int lena, lenb, lenc;
    17 string a, b, c;
    18 int dp[maxn][maxn];
    19 int main() {
    20     int n;
    21     cin >> n;
    22     while (n--) {
    23         cin >> a >> b >> c;
    24         int p = 0, q = 0;
    25         lena = a.size();
    26         lenb = b.size();
    27         lenc = c.size();
    28         if (lena + lenb != lenc) {
    29             cout << "No" << endl;
    30             continue;
    31         }
    32         for (int i = 0; i <= lena; i++) {
    33             for (int j = 0; j <= lenb; j++) {
    34                 dp[i][j] = !i & !j;
    35                 if (i && dp[i - 1][j] && a[i - 1] == c[i + j - 1]) {
    36                     dp[i][j] = 1;
    37                 }
    38                 if (j && dp[i][j-1] && b[j - 1] == c[i + j - 1]) {
    39                     dp[i][j] = 1;
    40                 }
    41             }
    42         }
    43         puts( dp[lena][lenb] ? "Yes": "No");
    44 
    45     }
    46     return 0;
    47 }

     链接:https://blog.csdn.net/Edviv/article/details/107402334

  • 相关阅读:
    SpringBoot进阶教程(六十七)RateLimiter限流
    Nginx限流配置
    ab test压力测试
    Nginx负载均衡配置
    arduino串口通信
    flask文件路径设置问题
    WIN10和ubunu共享文件夹相互访问
    树莓派录音和播放声音
    深度学习论文翻译解析(十六):Squeeze-and-Excitation Networks
    人工智能必备数学基础:高等数学基础(2)
  • 原文地址:https://www.cnblogs.com/0211ji/p/13335072.html
Copyright © 2011-2022 走看看