1 solutions

  • 0
    @ 2022-2-2 17:20:02
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<string>
    
    using namespace std;
    
    const int N = 1e4 + 5;
    char p[N], s[N];
    int ne[N];
    
    int main(void)
    {
    	while (~scanf("%s", p + 1))
    	{
    		int cnt = 0;
    		if (p[1] == '#'&&strlen(p+1)==1)break;
            //保证第一个字符是'#'并且只有一个字符的时候结束输入
    		scanf("%s", s + 1);
    		int len1 = strlen(p + 1);
    		int len2 = strlen(s + 1);
    
    		for (int i = 2, j = 0; i <= len2; i++)
    		{
    			while (j && s[i] != s[j + 1]) j = ne[j];
    			if (s[i] == s[j + 1]) j++;
    			ne[i] = j;
    		}//求ne数组
    
    		for (int i = 1, j = 0; i <= len1; i++)
    		{
    			while (j && p[i] != s[j + 1]) j = ne[j];
    			if (p[i] == s[j + 1]) j++;
    			if (j == len2)
    			{
    				cnt++;
    				j = 0;//剪掉一个j就要归零一次
    			}
    		}
    
    		cout << cnt << endl;
    		
            memset(p + 1, 0, sizeof(p));
    		memset(s + 1, 0, sizeof(s));//多组输入,那你就必须得要初始化字符串
    	}
    
    
    
    	return 0;
    }
    
    • 1

    Information

    ID
    264
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    6
    Tags
    # Submissions
    38
    Accepted
    14
    Uploaded By