zoukankan      html  css  js  c++  java
  • 一道字节跳动面试题——判断字符串交错

     

    判断字符串交错

    给定三个字符串a, b, c, 判断c是否可以通过字符串a, b的子串按顺序交错组成. 
    例如:
    a: "btdne" b: "yeac" c: "bytedance"
    返回: True
    a: "bytece" b: "dan" c: "bytedance"
    返回: True
    a: "bytec" b: "dan" c: "bytedance"
    返回: False
    a: "byte" b: "danced" c: "bytedance"
    返回: False
     
    输入描述
    分别输入三行, 每行均为string类型
    输出描述
    True/False
    示例1

    输入

    btdne
    yeac
    bytedance

    输出

    True
     
     
    #include <iostream>
    #include<stdio.h>
    
    #define MAX_SIZE 0x500
    
    //a: "btdne" b: "yeac" c: "bytedance"
    //a: "bytece" b: "dan" c: "bytedance"
    //a: "bytec" b: "dan" c: "bytedance"
    
    void main()
    {
        char szStra[MAX_SIZE] = { 0 };
        char szStrb[MAX_SIZE] = { 0 };
        char szStrc[MAX_SIZE] = { 0 };
    
        scanf_s("%s", szStra, MAX_SIZE);
        scanf_s("%s", szStrb, MAX_SIZE);
        scanf_s("%s", szStrc, MAX_SIZE);
    
        /*scanf(szStra, "%s");
        scanf(szStrb, "%s");
        scanf(szStrc, "%s");*/
    
        char *pStra = szStra;
        char *pStrb = szStrb;
        char *pStrc = szStrc;
    
        bool bRet = false;
    
        while (*pStrc != 0)
        {
            if (*pStra != *pStrc)
            {
                break;
            }
    
            while (*pStra == *pStrc && *pStra != 0 && *pStrc != 0)
            {
                pStra++;
                pStrc++;
            }
    
            if (*pStrb != *pStrc)
            {
                break;
            }
    
            while (*pStrb == *pStrc && *pStra != 0 && *pStrc != 0)
            {
                pStrb++;
                pStrc++;
            }
        }
    
        if (*pStrc == 0)
        {
            bRet = true;
            printf("True
    ");
        }
        else
        {
            printf("False
    ");
        }
    
        system("pause");
    }
  • 相关阅读:
    Dbzoj#3188. [Coci 2011]Upit
    P1903 [国家集训队]数颜色 带修改莫队板子
    P2045 方格取数加强版
    P1402 酒店之王
    P4151 [WC2011]最大XOR和路径
    Orz YYB!
    Atcoder2167 Blackout
    P2939 [USACO09FEB]改造路Revamping Trails
    博弈论简单入门sb总结
    P3592 [POI2015]MYJ
  • 原文地址:https://www.cnblogs.com/predator-wang/p/12019464.html
Copyright © 2011-2022 走看看