wyh2000 and a string problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 492 Accepted Submission(s): 239
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?
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(T≤105)
, 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.
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
Source
Recommend
官方解析:
1001. wyh and string problem 本题是一道送分的字符串题。这里给出两种解法。 1.维护一个类似栈的结构,存储wyh2000实际看到的字符串。扫描整个字符串,每看到两个相邻的 v 就往栈中加入一个 w ;否则看到什么就加入什么。最后扫一遍看 wyh 是不是栈中元素组成的串的子序列即可。 2.我们寻找一个 i ,使得 si=v 且 si+1=v 并且 i 尽量小。然后我们寻找一个 j ,使得 sj=w 且 j 尽量小。我们再寻找一个 k ,使得 sk=y,min(i+1,j)<k 且 k 尽量小。然后我们寻找一个 l ,使得 sl=h,k<l 。如果我们能找到符合条件的 i,k,l 或符合条件的 j,k,l ,那么答案就是 Yes ,否则答案就是 No 。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX= 3145728+5;
char a[MAX];
int main()
{
int t;
char tar[4]="wyh";
scanf("%d",&t);
while(t--)
{
int flag=0;
int i=0,j=0,k=0;
scanf("%s",a);
for(i=0;a[i+1];i++)
{
if((a[i]=='v'&&a[i+1]=='v'))
{
a[i]='w';
}
if(a[i]==tar[k])
{
k++;
}
if(k==3)
{
flag=1;
break;
}
}
for(;a[i];i++)
{
if(a[i] == tar[k]) k++;
if(k == 3)
{
flag=1;
break;
}
}
if(flag)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
中文题在下面:
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