HDU 5284 wyh2000 and a string problem(查找字符)

wyh2000 and a string problem

 Accepts: 428
 
 Submissions: 1313
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 131072/65536 K (Java/Others)

Problem Description

Young theoretical computer scientist wyh2000 is teaching young pupils some basic concepts about strings.

A subsequence of a string  s  is a string that can be derived from  s  by deleting some characters without changing the order of the remaining characters. You can delete all the characters or none, or only some of the characters.

He also teaches the pupils how to determine if a string is a subsequence of another string. For example, when you are asked to judge whether  wyh  is a subsequence of some string or not, you just need to find a character  w , a  y , and an  h , so that the  w  is in front of the  y , and the  y  is in front of the  h .

One day a pupil holding a string asks him, "Is  wyh  a subsequence of this string?" However, wyh2000 has severe myopia. If there are two or more consecutive character v s, then he would see it as one  w . For example, the string  vvv  will be seen as  w , the string  vvwvvv  will be seen as  www , and the string  vwvv  will be seen as  vww .

How would wyh2000 answer this question?

Input

The first line of the input contains an integer  T(T105) , denoting the number of testcases.

N  lines follow, each line contains a string.

Total string length will not exceed 3145728. Strings contain only lowercase letters.

The length of hack input must be no more than 100000.

Output

For each string, you should output one line containing one word. Output  Yes  if wyh2000 would consider  wyh  as a subsequence of it, or  No  otherwise.

Sample Input
4
woshiyangli
woyeshiyangli
vvuuyeh
vuvuyeh
Sample Output
No
Yes
Yes
No
问题描述
青年理论计算机科学家wyh2000在教小学生一些基础的字符串概念。
定义一个字符串
  
  
   
   s
  
  的子序列为将
  
  
   
   s
  
  中一些元素删掉得到的字符串。可以删掉全部元素,可以不删,也可以只删一些。
他还教了小学生如何判断一个串是不是另一个串的子序列。比如给你一个串,要求判断
  
  
   
   wyh
  
  是不是它的子序列,那么你只需要找一个
  
  
   
   w
  
  ,找一个
  
  
   
   y
  
  ,再找一个
  
  
   
   h
  
  ,使得
  
  
   
   w
  
  
  
  
   
   y
  
  前面,
  
  
   
   y
  
  
  
  
   
   h
  
  前面即可。
有一天小学生拿着一个串问他“
  
  
   
   wyh
  
  是不是这个串的子序列?”
但是wyh2000有重度近视眼,如果字符串中有一段连续的
  
  
   
   v
  
  (至少两个),那么他会把它看成一个
  
  
   
   w
  
  。例如,字符串
  
  
   
   vvv
  
  会被看成
  
  
   
   w
  
  ,字符串
  
  
   
   vvwvvv
  
  会被看成
  
  
   
   www
  
  ,字符串
  
  
   
   vwvv
  
  会被看成
  
  
   
   vww
  
  。
请问wyh2000会怎么回答这个问题?
输入描述
第一行为数据组数
  
  
   
   T(1T105)
  
  。
接下来
  
  
   
   T
  
  行,每行一个字符串,表示小学生拿来问wyh2000的串。
总串长不超过3145728。只包含小写字母。
hack数据字符串长度不超过100000。
输出描述
对于每组数据,如果wyh2000会把
  
  
   
   wyh
  
  看成该串的子串,那么输出一行
  
  
   
   Yes
  
  ,否则输出一行
  
  
   
   No
  
  
输入样例
4
woshiyangli
woyeshiyangli
vvuuyeh
vuvuyeh
输出样例
No
Yes
Yes
No

解题思路:题目简单的说就是问能否在一个字符串中依次找到‘w’,‘y’,‘h’或“vv”,‘y’,‘h’?既然是依次找到,那么我们就可以得知,如果‘w’或“vv”没有找到,那么就没必要找‘y’;同样的,如果‘w’和‘y'或“vv”和’y'没有找到,就没必要找‘h’。想明白这一点,就可以定义一个变量flag1用来标记‘w’或“vv”有没有找到,定义另一个变量flag2用来标记‘y’有没有找到,再定义一个变量flag3用来标记‘h’有没有找到。

代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-4)
#define inf (1<<28)
#define sqr(x) (x) * (x)
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
int main()
{
    string s;
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        cin>>s;
        int flag1=0,flag2=0,flag3=0;
        for(i=0;i<s.size();i++)
        {
            if(s[i]=='w')
                flag1=1;
            if(s[i]=='v'&&s[i+1]=='v')
                flag1=1;
            if(s[i]=='y'&&flag1==1)
                flag2=1;
            if(s[i]=='h'&&flag2==1)
                flag3=1;
        }
        if(flag3)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值