wyh2000 and a string problem
Accepts: 428
Submissions: 1313
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 131072/65536 K (Java/Others)
问题描述
青年理论计算机科学家wyh2000在教小学生一些基础的字符串概念。 定义一个字符串 s 的子序列为将 s 中一些元素删掉得到的字符串。可以删掉全部元素,可以不删,也可以只删一些。 他还教了小学生如何判断一个串是不是另一个串的子序列。比如给你一个串,要求判断 wyh 是不是它的子序列,那么你只需要找一个 w ,找一个 y ,再找一个 h ,使得 w 在 y 前面, y 在 h 前面即可。 有一天小学生拿着一个串问他“ wyh 是不是这个串的子序列?” 但是wyh2000有重度近视眼,如果字符串中有一段连续的 v (至少两个),那么他会把它看成一个 w 。例如,字符串 vvv 会被看成 w ,字符串 vvwvvv 会被看成 www ,字符串 vwvv 会被看成 vww 。 请问wyh2000会怎么回答这个问题?
输入描述
第一行为数据组数 T(1≤T≤105) 。 接下来 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;
}