有两个字符串s 和t,如果即从s 中删除一些字符,将剩余的字符连接起来,即可获得t。则称t是s 的子序列。
请你开发一个程序,判断t是否是s的子序列。
输入包含多组数据,每组数据包含两个字符串s和t。
它们都由数字和字母组成,且长度小于100000。
对应每一组输入,如果t是s的子序列,则输出“Yes”;否则输出“No”。
ABC ABC ABC AB ABC DE
Yes Yes No
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
int main()
{
string a,b;
cin>>b;
cin>>a;
for(int i=0;i<b.size();i++)
{
if(a[0]==b[i])
{
for(int j=0;j<a.size();j++)
{
if(a[j]!=b[i+j]||i+j>=b.size())
{
break;
}
cout<<"Yes"<<endl;
return 0;
}
}
}
cout<<"No"<<endl;
return 0;
}
#include <cstdio>
#include <limits.h>
#include <vector>
#include <functional>
#include <string>
#include <iostream> using namespace std; int main()
{
//freopen("in.txt", "r", stdin);
string s, t;
while (cin >> s >> t)
{
if (s.size() < t.size())
{
cout << "No" << endl;
continue;
}
int i = 0, j = 0;
while (i < s.size() && j < t.size())
{
if (s[i] == t[j]) ++j;
++i;
}
cout << (j == t.size() ? "Yes" : "No") << endl;
} return 0;
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String s1=in.next();
String s2=in.next();
int len1=s1.length();
int len2=s2.length();
char[] cA1 = s1.toCharArray();
char[] cA2 = s2.toCharArray();
int i = 0, j = 0;
for(; i<len1 && j<len2;)
{
if(cA1[i] == cA2[j])
{
j++;
}
i++;
}
if(j == len2)
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
}
#include<cstdio>
(802)#include<cstring>
int main(){
char str1[100010],str2[100010];
while(scanf("%s %s",str1,str2)!=EOF){
int k=0;
int len1=strlen(str1);
int len2=strlen(str2);
for(int i=0;i<len1&&k<len2;i++){
if(str1[i]==str2[k])
{
k++;
}
}
if(k==len2){
printf("Yes\n");
}else{
printf("No\n");
}
}
return 0;
} #include <iostream>
#include <string.h>
using namespace std;
string allInAll(string s, string t) {
int i = 0;
int j = 0;
int tLen = (int)t.length();
while (s[i]&&t[j]) {
if (s[i] == t[j]) {
i++;
j++;
} else {
i++;
}
}
if (j == tLen) {
return "Yes";
} else {
return "No";
}
}
int main() {
string s, t;
while (cin>>s>>t) {
cout<<allInAll(s, t)<<endl;
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char a[100001], b[100001];
while(scanf("%s%s", a, b) != EOF) {
int i, j, lena = strlen(a), lenb = strlen(b);
for(i = j = 0; i < lena && j < lenb; ++i) {
if(a[i] == b[j]) {
++j;
}
if(j == lenb){
printf("Yes\n");
break;
}
}
if(j < lenb)
printf("No\n");
}
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str, res;
while (cin >> str >> res)
{
int j = 0;
bool sta = false;
for (auto c : str)
{
if (c == res[j])
j++;
if (j == res.length())
{
sta = true;
break;
}
}
if (sta == true)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
while(read.hasNext())
{
String str1 = read.next();
String str2 = read.next();
judgeStr(str1, str2);
}
read.close();
}
/**
*
* @param source
* @param target
* @return
*/
public static int indexOf(char[] source, char[] target) {
int targetCount = target.length;
int sourceCount = source.length;
if (targetCount == 0) {
return 0;
}
char first = target[0];
int max = sourceCount - targetCount;
for (int i = 0; i <= max; i++) {
if (source[i] != first) {
while (++i <= max && source[i] != first)
;
}
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = 1; j < end
&& source[j] == target[k]; j++, k++)
;
if (j == end) {
return i;
}
}
}
return -1;
}
public static void judgeStr(String str1, String str2)
{
int len1 = str1.length(), len2 = str2.length();
int i = 0, j = 0;
for(; i<len1 && j<len2;)
{
if(str1.charAt(i) == str2.charAt(j))
{
j ++;
}
i ++;
}
if(j == len2)
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
#include <iostream>
#include <string>
using namespace std;
{
int len1 = str1.length(), len2 = str2.length();
int i, j; //定义str1和str2的循环变量
for(i = 0, j = 0; i < len1 && j < len2;)
{
//如果str1[i] == str2[j],则移到str2的下一位
if(str1[i] == str2[j])
j ++;
i ++; //移到str1的下一位
}
if(j == len2) //如果str2遍历完毕,表示str1中存在str2的所有字符
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main()
{
string s1, s2;
while(cin >> s1 >> s2)
jugeStr(s1, s2);
return 0;
}