POJ 1936 All in All(string)

All in All

Time Limit: 1000MS   Memory Limit: 30000K
     

Description

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.

Input

The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII characters separated by whitespace.The length of s and t will no more than 100000.

Output

For each test case output "Yes", if s is a subsequence of t,otherwise output "No".

Sample Input

sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter

Sample Output

Yes
No
Yes
No

题目大意:

问是否字符串1是字符串2的子列。

解题思路:

对第一个字符串进行枚举,然后和第二个字符串匹配,匹配正确记录位置,然后进行下一个字符的匹配。(匹配是找离当前最近的并且靠右的)。

代码:

#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

string str1,str2;

void solve(){
    bool ans = true;
    int pos=0;
    for(int i=0;i<str1.length();i++){
            for(int j=pos;j<str2.length();j++){
                if(str1[i]==str2[j]){
                    pos=j+1;
                    break;
                }
                if(j==str2.length()-1&&str2[j]!=str1[i]){
                    ans =false;
                }
            }
    }
    if(!ans) printf("No\n");
    else printf("Yes\n");
}

int main(){
    while(cin>>str1>>str2){//cin都是以空格,tab,换行结束的,getline是以换行结束的.
        solve();
    }
    return 0;
}
时间: 2024-10-12 21:14:08

POJ 1936 All in All(string)的相关文章

[ACM] POJ 1936 All in All (查找,一个串是否在另一个串中)

All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27521   Accepted: 11266 Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

POJ 3069 Saruman&#39;s Army (贪心)

题目大意:直线上有N个点,点i的位置是Xi,从这N个点中选取若干,给他们加上标记,对每一个点,其距离为R以内的区域内必须有被标记的点.求至少需要多少个点被标记. 题目思路:设最左边的点:点p的坐标为x,那么离其距离为R的点的坐标为(x+R),我们应该标记的点应为坐标最接近且小于等于(x+R)的点p,则此时[x,p+R]范围内点点均被标记.依次进行下去,直到包含所有点为止. #include<stdio.h> #include<queue> #include<iostream&

POJ 1068--Parencodings--括号逆匹配(模拟)

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19655   Accepted: 11870 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn

poj A Knight&#39;s Journey(DFS)

题目链接:http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=190592 题意:给出p*q的棋盘,从(A,1)开始,走“日”字,问能否走完棋盘上所有的点,如果能,按字典序输出路径: 思路:DFS,并保存路径即可,注意处理走的方向顺序int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; #include <stdio.h> #in

poj Problem A. Score Sequence(暴力)

这道题,对于我这种英文不好的人来说,有点费劲啊. 题目的意思:给你两组成绩,你要找出他们之间最大的公共子序列,不能有重复,然后输出两组数据. 第一组就是:按照从大到小输出最大子序列. 第二组就是:按照个位数由小到大输出,若个位数一样大,则小的在前输出最大子序列. 解题思路基本上已经出来了,就是千万要注意就是在最长子序列匹配之前就就把重复的数字给删除. 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h

POJ 3128 Leonardo&#39;s Notebook (置换)

Leonardo's Notebook Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2324   Accepted: 988 Description - I just bought Leonardo's secret notebook! Rare object collector Stan Ucker was really agitated but his friend, special investigator Sa

POJ 2488-A Knight&#39;s Journey(DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10813 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar

zoj 2358,poj 1775 Sum of Factorials(数学题)

题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n能否表示成几个数的阶乘之和 //这里有一个重要结论:n!>(0!+1!+……+(n-1)!), //证明很容易,当i<=n-1时,i!<=(n-1)!,故(0!+1!+……+(n-1)!)<=n*(n-1)!=n!. // 由于题目规定n<=1000000,而10!=362880