每日算法4 —— UVa10340 子序列 All in All

一、Question

1. 题目描述

You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way. Because of pending patent issues we will not discuss in detail how the strings are generated and inserted into the original message. To validate your method, however, it is necessary to write a program that checks if the message is really encoded in the final string.
Given two strings s and t, you have to decide whether s is a subsequence of t, i.e. if you can remove characters from t such that the concatenation of the remaining characters is s.
此题要求我们,输入两个字符串s和t,判断是否可以从t中删除0个或多个字符(其它字符顺序不变),得到字符串s。例如,abcde可以得到bce,但无法得到dc。

2. Input

The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII characters separated by whitespace. Input is terminated by EOF.

3. Output

For each test case output, if s is a subsequence of t.

4. Sample Input

sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter

5. Sample Output

Yes
No
Yes
No

二、 题解

最简单的思想就是循环嵌套,即从头到尾扫描字符串s,每扫到s的每个字符,就在t中遍历寻找该字符,下一次在t中寻找时,从该字符的下一个索引开始(因为要求是有次序的)。但是这种蛮力法可能存在一定缺陷,很可能不能AC全部检查点,因为时间复杂度是O(M^N^),如果有要求的话,可以考虑KMP算法,或者改进的KMP算法

1. C++

#include
using namespace std;
int main()
{
    string s, t;
//    s.resize(105);
//    t.resize(105);
    while (cin >> s >> t){
        int j = 0;
        int flag = 1;
        int p;
        for(int i = 0; i < s.length(); ++i)
        {
            for(p = j; p < t.length(); ++p)
            {
                if(t[p] == s[i])
                {
                    j = p + 1;
                    break;
                }
                else
                    continue;
            }
            if(p == t.length())
            {
                flag = 0;
                break;
            }
        }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
}

2. Python

if __name__ == '__main__':
    while True:
        # s = input()
        # t = input()
        s, t = map(str, input().split())
        j = 0
        flag = 1
        p = 0
        k = 0
        for i in range(len(s)):
            for p in range(j, len(t)):
                if s[i] == t[p]:
                    j = p + 1
                    break
                else:
                    k = p + 1
                    continue
            if k == len(t):
                flag = 0
                break
        if flag == 1:
            print('Yes')
            # print('\n')
        else:
            print('No')
            # print('\n')

虽然博主不得不承认这的确是一道水题,但是的确踩了一些坑(译:菜得很) (1)C++不建议使用scanf输入字符串,因为一般地,C++中scanf输入字符串的用法是:s.resize(105); scanf("%s", &s[0]); 这就会产生一个问题:你已经resize了105个空间,即代表s.length()就是105,因此之后就会产生很大的问题。(应该不用继续说了叭~) (2)在上面的C++代码中,如果没有break出循环的话,循环一定能跑完,即最后p一定等于t.length() (3)PYTHON程序中,由于每组输入两个字符串,且用空格分隔,因此不宜使用input()函数,而建议使用map函数来输入所需要的数据(字符串)。(split可以设置分隔符)

你可能感兴趣的:(算法,算法,c++,python)