Did Li Hua go to college? (I)

Title description
Although Xiaobo has already graduated from high school, he involuntarily pays attention to the development of the college entrance examination during the college entrance examination season. It is also the first time to know that the old friend of the year in the 2020 college entrance examination English paper is missing: Li Hua. Xiaobo couldn't help sighing: Qingjie is over!
Now give a long string s, which includes uppercase and lowercase letters, symbols, spaces, etc., and ask whether it contains the string "Did Li Hua go to university" (without quotation marks).
If it contains, output "Yes", otherwise output " No" (without quotation marks).
Enter
an integer t in the first line, representing t groups of data.
In the next t line, each line has a string s, which includes uppercase and lowercase letters, symbols, spaces, etc., and the length is less than 1e5.
Output
Output t lines, if each line contains "Did Li Hua go to university" (without quotation marks), then output "Yes", otherwise output "No" (without quotation marks).
Sample input
2
Did Li Hua go to university, Xiaobo?
Did you go to university, Xiaobo?
Sample output
Yes
No

When I started to do this question, I used a violent if judgment, which was a waste of time for us to type the program. It is not recommended

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
#define N 150
using namespace std;
int main()
{
    
    
    int t,flag;
    char s[100000];
    cin>>t;
    for(int j=0;j<=t;j++)
    {
    
    
        gets(s);
        flag=0;
        for(int i=0;s[i]!='\0';i++)
        {
    
    
            if(s[i]=='D'&&s[i+1]=='i'&&s[i+2]=='d')
            {
    
    
                if(flag==0&&s[i]=='D'&&s[i+1]=='i'&&s[i+2]=='d'&&s[i+3]==' '&&s[i+4]=='L'&&s[i+5]=='i'&&s[i+6]==' '&&s[i+7]=='H'&&s[i+8]=='u'&&s[i+9]=='a'&&s[i+10]==' '&&s[i+11]=='g'&&s[i+12]=='o'&&s[i+13]==' '&&s[i+14]=='t'&&s[i+15]=='o'&&s[i+16]==' '&&s[i+17]=='u'&&s[i+18]=='n'&&s[i+19]=='i'&&s[i+20]=='v'&&s[i+21]=='e'&&s[i+22]=='r'&&s[i+23]=='s'&&s[i+24]=='i'&&s[i+25]=='t'&&s[i+26]=='y')
                {
    
    
                    flag=1;
                    cout<<"Yes"<<endl;
                    i+=25;
                }
            }
            if(flag==0&&s[i+1]=='\0')cout<<"No"<<endl;
        }
    }
}

There is a better way:
Analysis : Use the find function of string directly

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
#define N 150
using namespace std;
int main()
{
    
    
    int t;
    string s;
    cin>>t;
    getchar();//注意开一个getchar承接上一条语句的回车
    while(t--)
    {
    
    
        getline(cin,s);//字符串输入函数
        if(s.find("Did Li Hua go to university")<s.size())cout<<"Yes"<<endl;//用find函数进行查找。
        //若找到则会返回一个值,则一定会小于整个字符串的长度。
        //反之返回的值一定会远远大于字符串长度
        else cout<<"No"<<endl;
    }
    return 0;
}

The description of the find function:
s.find(str,M) represents the string starting from the subscript M to search for str. If M is not specified, that is, find(str), the default is to start the search from the subscript 0. If found, return the starting subscript (it must be less than the total string length, this is the idea of ​​this question), if not found, return a npos, this value is very large, no need to explain too much, just need to know if it can’t be found , Then the returned value must be greater than the length of a string.

Guess you like

Origin blog.csdn.net/m0_52380556/article/details/115314083