523C Name Quest【挡板,字符串】

C. Name Quest
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, ifsaba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.

However rather than being happy once, he loves twice as much being happy twice! So, when he got stringt as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.

Help s determine the number of distinct ways to cut the given stringt into two parts in the required manner.

Input

The first line contains string s, consisting of lowercase English letters. The length of strings is from 1 to 1000 letters.

The second line contains string t, that also consists of lowercase English letters. The length of stringt is from 1 to 106 letters.

Output

Print the sought number of ways to cut string t in two so that each part mades happy.



Sample test(s)
Input
aba
baobababbah
Output
2
Input
mars
sunvenusearthmarsjupitersaturnuranusneptune
Output
0



题意:  俩字符串,,问    第二个字符串中分成两份,左半分和有半分都包含上面字符串,保证这个条件的    分法有多少种。。这里的包含指的是顺序必须对,里面可以插入其                  他数。。
                 比如给一个aba
                后面a……#……b&*&((a    就算包含。 因为aba三个字母都有,而且顺序也是跟上面顺序一样的。


分析:第一个是s1字符串(短的),第二个是s2字符串(长的)。。从左向右遍历s2,i=0向右循环,里面包含到完全的s1之后推出循环,即找到最左边的可以包含s1的那个长度i
从右边开始倒序遍历j=s2.size()-1,里面判断倒着包含s1,完全包含时长度为j。。
              这里用j-i与0取最大值。。注意:这里i是最左边完全包含s1字符串最少位数。j是最右边完全包含s1字符串的最少位数。这样相减得出的数就是这些数中间有多少个挡板,挡板问题,高中数学知识了。。



代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<algorithm>
#include <map>
#include <queue>
using namespace std;

//523C  间隔挡板问题
int main()
{

    while(true)
    {
       string s1,s2;
        int m,n;
        int o,p;
        int i,j;
        cin>>s1>>s2;
        m=s1.size();
        n=s2.size();
        o=0;
        p=0;
        for(i=0;i<n;i++)
        {
            if(s2[i]==s1[o])o++;
            if(o==m)break;
        }
        for(j=n-1;j>=0;j--)
        {
            if(s2[j]==s1[m-1-p])p++;
            if(p==m)break;
        }
        cout<<max(j-i,0)<<endl;
    }

    return 0;
}



你可能感兴趣的:(523C Name Quest【挡板,字符串】)