心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0
题目大意:给出两个字符串s、t,判断s是否是t的子序列。
按顺序在t中寻找每一个s中的字符即可,每次记录该字符在上一个字符出现位置之后的第一次出现的位置。
以下是我的代码:
#include<iostream>
#include
<string>
#include
<cstdio>
using namespace std;

bool OK(const string &s,const string &t)
{
    
int pos(-1);
    
for(int i=0;i<s.size();i++)
    {
        
bool found(false);
        
for(int j=pos+1;j<t.size();j++)
            
if(s[i]==t[j])
            {
                pos
=j;
                found
=true;
                
break;
            }
        
if(!found)
            
return false;
    }
    
return true;
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen(
"data.in","r",stdin);
    freopen(
"data.out","w",stdout);
    
#endif

    
string s,t;
    
while(cin>>s>>t)
        
if(OK(s,t))
            cout
<<"Yes"<<endl;
        
else
            cout
<<"No"<<endl;

    
return 0;
}
posted on 2011-05-21 08:22 lee1r 阅读(359) 评论(0)  编辑 收藏 引用 所属分类: 题目分类:基础/模拟

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理