zoukankan      html  css  js  c++  java
  • HDU 2087 剪花布条【在字符串中不可重叠地寻找子串数量】

    一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? 

    Input输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。 
    Output输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。 
    Sample Input

    abcde a3
    aaaaaa  aa
    #

    Sample Output

    0
    3

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #define MAX 1000 + 10
    using namespace std;
    
    char a[MAX], b[MAX];
    int p = 0;
    
    int main()
    {
        while ( scanf("%s", a), a[0] != '#') {
            getchar();
            scanf("%s", b);
            getchar();
            int ans = 0;
            char *pa = a, *pb = b;
    
            while ( strstr( pa, pb)) {
                ans ++;
                pa = strstr( pa, pb);
                if ( strlen( pa) == strlen( pb)) {
                    break;
                }
                pa += strlen( pb);
            }
            printf("%d
    ", ans);
        }
    
    
        return 0;
    }
    库函数strstr的运用
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define M 10010
    #define N 1000010
    char s1[M],s2[N];
    int next1[N];
    int sum;
    void Nextll(int len2)
    {
       int i=-1,j=0;
       next1[0]=-1;
       while(j<len2)
        {
           if(i==-1||s2[i]==s2[j])
           {
               ++i;
               ++j;
               next1[j]=i;
           }
           else
               i=next1[i];
        }
    }
    void Kmp(int len1,int len2)
    {
       int i=0,j=0;
       sum=0;
       while(j<len1)
        {
           if(i==-1||s1[j]==s2[i])
           {
               ++i;
               ++j;
           }
           else
               i=next1[i];
           if(i==len2)
           {
               sum++;
               i=next1[i];
           }
        }
    }
    int main()
    {
       while(~scanf("%s%s",s1,s2))
        {
            if((strcmp(s1,"#")==0)||(strcmp(s2,"#")==0))
                     break;
           int len1=strlen(s1);
           int len2=strlen(s2);
           Nextll(len1);
           Kmp(len1,len2);
           printf("%d
    ",sum);
        }
       return 0;
    }
    kmp
  • 相关阅读:
    用Photoshop制作一寸照片
    每天只问孩子这4句话,胜过百般疼爱
    机场也有打折季,你知道吗?请收好这份扫货指南
    这8个习惯会让孩子越来越笨,甚至抑郁!父母赶紧收手
    读后感该怎么写
    vue-cli 4058错误
    bootstrap img自适应
    移动端高清、多屏适配方案
    去掉页面滚动条
    js 404页面跳转
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7499137.html
Copyright © 2011-2022 走看看