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

  • 相关阅读:
    【转】[fix] Wireshark error: There are no interfaces on which a capture can be done. on Mac OS X
    【转载】Linux 文件系统的目录结构
    postgreSQL使用
    [转载] Linux启动过程详解-《别怕Linux编程》之八
    冒泡排序
    java值类型和引用类型
    冒泡排序法与二分查找法
    关系型数据库
    SQList的建表并添加数据练习
    数据存储——SQLite数据库存储
  • 原文地址:https://www.cnblogs.com/0211ji/p/13335072.html
Copyright © 2011-2022 走看看