【刘汝佳书】习题3-9 UVA10340

UVA刷题方法:https://blog.csdn.net/richenyunqi/article/details/80990535#commentBox

【2019.3.26】
思路:先输入字符串s,再用getchar一个个读入字符串t
每读入一个字符串t中的字符c,用s[i](初始i=0)跟其对比,如果相等的话,i++
最终当字符串t全部读入,如果这时i==strlen(s),证明s中的每个字符都能在t中找到顺序相同的相同字符

每次遇到这种题,题中没说输入测例的大小,在代码中开数组的时候总会有点虚,怕输入的字符串太长导致越界

#include <iostream>
#include <cstring>
using namespace std;

char sub[3000];
char s[3000];

int main()
{
    char c;
    int i;
    while(scanf("%s",sub)!=EOF) {
        getchar();
        i=0;
        while((c=getchar())&&(c!='\n')) {
            if(i<strlen(sub) && sub[i]==c) i++;
        }
        if(i==strlen((sub))) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41727666/article/details/88813698