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

来源:互联网 发布:李炎恢php第四季下载 编辑:程序博客网 时间:2024/04/29 01:14

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

解题思路:题目简单的说就是问能否在一个字符串中依次找到‘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
原创粉丝点击