poj 1396 All in All 字符串处理,短小精悍的题目

All in All
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 29876 Accepted: 12434

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

Source


wa了三次,第一次对while的那句的p++,理解不深刻,导致for中没有p++;
                第二次对while的两种情况没有分开处理
              第三次“Yes”莫名奇妙的写成了“yes”
  还c e了一次 ,网速原因。

题意:给出字符串  b和a,如果b是a的子串(b字符在a中的位置可以不相邻)输出Yes,否则No;
思路:
解法是对于b的字符在a中从左往右开始进行匹配,找到a中与之匹配的的第一个字符x,就开始匹配b中的下一个字符,同时要把x从下次匹配的范围内去掉。
如果b的字符全部匹配,那么Yes,否则No。

原理:如果b中一个字符匹配到a中较后的位置时,整个b可以匹配成功,那么这个字符匹配到a中较前的位置也一定能成功。
           (最优的匹配方式)
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mp    make_pair
#define _f     first
#define _s     second

using namespace std;
const int INF =0x3f3f3f3f;
//const int maxn=    ;
//const int maxm=    ;
//const int INF=    ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
//by yskysker123
char b[100000+10];
char a[100000+10];


int main()
{

    while(~scanf("%s%s",&b,&a))
    {
        int p=0,i;
        for( i=0;b[i]&&a[p];i++,p++)   
        {
            while(a[p]&&a[p]!=b[i])  p++;//细节:既要考虑不越界,又要对同种结果的各种情况心中有数,做出相应的处理
            if(!a[p])  break;   //如果没这句话,abc   abgg会输出Yes  (abc  ab  输出"No")
        }
        if(!b[i])  puts("Yes");
        else puts("No");
    }

    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值