兔子的名字

题目

ac代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <math.h>
#define endl '\n'
using namespace std;
int n,m,sum;
struct tz
{
    
    
	string t;
	string s;
}a[1009];
int main()
{
    
    
    ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(int i=0;i<n;i++) cin >> a[i].t;
    for(int i=0;i<m;i++) cin >> a[i].s;
    for(int i=0;i<n;i++)
    {
    
    
    	
    	sum=0;
    	string s1 = a[i].t;
    	for(int j=0;j<m;j++)
    	{
    
    
    		string s2 = a[j].s;
            int cnt=0,pos=-1;//定义一个变量pos 来向后遍历子串
    		
    		for(int k=0;k<a[j].s.size();k++)
    		{
    
    
    			if(   (pos=s1.find(s2[k],pos+1))   !=-1)cnt++;//  括号里的很关键  易错
                if(cnt==s2.length()){
    
    sum++;break;
                }
			}
		}
		cout << sum << endl;
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/cosx_/article/details/112199918