Topic describes

She heard an interesting piece of news from her friend Akiu Barutan: a provincial competition was being held in an outside place called “Liaoning Province”. Little Ling was curious about the place called “Liaoning Province”. Xiao Ling wants to know more about Liaoning Province, so she finds a lot of outside information. In order to determine which materials are relevant to Liaoning Province, she came up with a method to determine the relevance of materials. First, Bell picked out n words, believing that the relevance of the data was only related to the number of times the n words appeared. Ling then looked for the words in the data and recorded how many times they appeared, and the more times they appeared, the more relevant she thought the data were to Northeastern University. Now, the small bell has chosen the N words, is ready to start reading materials, barnyard field Qiu came to her to borrow books. Can you help Bell finish reading the material and tell bell how many times the word that bell chooses appears in the material? PS: source: 2021 in liaoning province collegiate programming contest (friendly) link: ac.nowcoder.com/acm/contest…

Input description:

The first line of data contains an integer T(1≤T≤101 \le T \le101≤T≤10), which represents the number of groups of data. For each set of data: The first line contains an integer n(1≤n≤101\le n \le 101≤n≤10), representing the total number of words selected by the bell. The next n lines are a string of only lowercase letters of length W (1≤ W ≤101\le w \le 101≤w≤10), representing the n words selected by the bell. Next, a string of length s(1≤ S ≤1041 \le s \le 10^41≤ S ≤104), consisting of only lowercase letters, represents the contents of the data.Copy the code

Output description:

Each set of test data outputs a number indicating how many times the words selected by the bell appear in the data.Copy the code

Example 1

The input

2
2
neu
neuoj
neuisestablishedin1923neuojis2013
1
aa
aaa
Copy the code

The output

3
2
Copy the code

Thought analysis

I have two solutions to a typical string matching problem. One is to use the C++ STL find function, which returns the subscript position of w[I] in s. The default is the first occurrence of the position. Use times to record on ++ AC code 2 is a routine complex operation, the likes of more than 3 to analyze, leave a pit

AC code 1

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T,n,t,times;
    string w[10],s;
    cin>>T;
    while(T--)
    {
        times=0;
        cin>>n;
        for(int i=0; i<n; i++) cin>>w[i]; cin>>s;for(int i=0; i<n; i++) { int position=s.find(w[i]);while(position! =s.npos) { times++; position=s.find(w[i],position+1);// select position+1 from w[I]
            }
        }
        cout<<times<<endl;
    }
    return 0;
}
Copy the code

AC code 2

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T,n,t,flag,times;
    string w[10],s;
    cin>>T;
    while(T--)
    {
        times=0;
        cin>>n;
        for(int i=0; i<n; i++) cin>>w[i]; cin>>s;for(int j=0; j<s.length(); j++) {for(int k=0; k<n; k++) { t=j; flag=0;
                for(int i=0; i<w[k].length(); i++) {if(s[t++]! =w[k][i]) flag=1;
                }
                if(flag==0)
                    times++;
            }
        }
       cout<<times<<endl;
    }
    return 0;
}
Copy the code

Conclusion comprehension

Where there is life, there is code!