hdu 3336

题意:输入一个字符串求每个前缀在串中出现的次数和

sol:只要稍微理解下next 数组的含义就知道只要把每个有意义的next值得个数加起来即可

PS:网上有dp解法orz,dp[i]表示以i为前缀串结尾的前缀串的总和,方程很容易写出

//字符串上KMP(水)
//从前向后扫,失配函数的位置就是一个前缀的位置减1
//加起来就好了
// by acvc
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX = ;
const int MOD = ;
char str[MAX];
int next[MAX],vis[MAX];
int main()
{
    int cas,n;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d %s",&n,str);
        next[]=next[]=;
        for(int i=;i<n;i++)
        {
            int j=next[i];
            while(j&&str[i]!=str[j]) j=next[j];
            if(str[i]==str[j])
            next[i+]=j+;
            else next[i+]=;
        }
        int ans=,cnt=;
        for(int i=;i<n;i++)
        {
            if(next[i])
            {
            //    cnt++;
                ans=(ans+)%MOD;
            }
            else
            ans=(ans+)%MOD;
        }
        if(next[n]) ans=(ans+)%MOD;
        printf("%d\n",(ans)%MOD);
    }
    return ;

}

hdu 1358

题意:给出一个字符串求出每个前缀的最小周期

sol:next数组理解题目稍微想想就知道t=(len-next[len])

//kmp小深入题目
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = +;
char str[MAX]; int next[MAX]; //失配函数
int main()
{
    int n,cnt=;
    while(scanf("%d",&n)>)
    {
        scanf("%s",str);
        next[]=next[]=;
        for(int i=;i<n;i++)
        {
            int j=next[i];
            while(j&&str[i]!=str[j]) j=next[j];
            if(str[i]==str[j]) next[i]=j+;
            else next[i]=;
        }
        printf("Test case #%d\n",++cnt);
        for(int i=;i<=n;i++)
        {
            if(next[i]&&i%(i-next[i])==)
            printf("%d %d\n",i,i%(i-next[i]));
        }
    }
    return ;

}

hdu1711

题意:给出两个数组,求出b在a中最先匹配的位置

sol:KMP裸题

 1 //裸题
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 const int MAX = 1e6+;
 7 int next[MAX];
 8 int a[MAX],b[MAX];
 9 int main()
 {
     int cas,n,m;
     scanf("%d",&cas);
     while(cas--)
     {
         scanf("%d %d",&n,&m);
         for(int i=;i<n;i++) scanf("%d",&a[i]);
         for(int j=;j<m;j++) scanf("%d",&b[j]);
         next[]=next[]=;
         for(int i=;i<m;i++)
         {
             int j=next[i];
             while(j&&b[i]!=b[j]) j=next[j];
             if(b[i]==b[j]) next[i+]=j+;
             else next[i+]=;
         }
         int cur=,flag=;
         for(int i=;i<n;i++)
         {
             while(cur&&a[i]!=b[cur]) cur=next[cur];
             if(a[i]==b[cur]) cur++;
             if(cur==m)
             {
                 flag=;
                 printf("%d\n",i-cur+);
                 break;
             }
         }
         if(!flag) printf("-1\n");
     }
     return ;

41 }

hdu2087

题意:给定串1和串2求串2在串1中出现的顺序

sol;裸KMP从前向后扫一遍kmp就好了

 1 #include<cstring>
 2 #include<algorithm>
 3 #include<cstdio>
 4 using namespace std;
 5 const int MAX = +;
 6 char str1[MAX],str2[MAX];
 7 int next[MAX];
 8 int main()
 9 {
     while(scanf("%s",str1)&&strcmp(str1,"#"))
     {
         int ans=;
         scanf("%s",str2);
         int n=strlen(str2); next[]=next[]=;
         for(int i=;i<n;i++)
         {
             int j=next[i];
             while(j&&str2[i]!=str2[j]) j=next[j];
             if(str2[i]==str2[j]) next[i+]=j+;
             else next[i+]=;
         }
         int len=strlen(str1); int j=;
         for(int i=;i<len;i++)
         {
             while(j&&str1[i]!=str2[j]) j=next[j];
             if(str1[i]==str2[j]) j++;
             if(j==n)
             {
                 ans++;
                 j=;
             }
         }
         printf("%d\n",ans);
     }
     return ;

36 }

poj2406

题意:给定一个串求出串的最小周期

los:失配函数裸题啊

 1 //kmp-shui
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cstdio>
 5 using namespace std;
 6 const int MAX = 1e6+;
 7 char str[MAX]; int next[MAX];
 8 int main()
 9 {
     int ans;
     while()
     {
         gets(str); if(!strcmp(str,".")) break;
         int n=strlen(str); next[]=next[]=;
         for(int i=;i<n;i++)
         {
             int j=next[i];
             while(j&&str[i]!=str[j]) j=next[j];
             if(str[i]==str[j]) next[i+]=j+;
             else next[i+]=;
         }
         if(n%(n-next[n])==)
         printf("%d\n",n/(n-next[n]));
         else printf("1\n");
     }
     return ;

27 }

poj 2752

题意:给定一个串求出满足既是前缀又是后缀的串的起始位置

sol:又是一发next数组加深题目,很明显next数组指向的是最长的一个前缀串,所以最后一个指针指向的next就是一个最长前缀

之后从这个最长前缀末尾开始下一个指针又是前缀的最长前缀,而后缀和前缀相同,所以这个是第二长的前缀,只要递归结束即可

1 //kmp题目shui by acvc

 2 //kmp每次都是求的最长的前缀
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cstdio>
 6 #include<vector>
 7 using namespace std;
 8 const int MAX = +;
 9 int next[MAX];
 char str[MAX];
 vector<int> s;
 int main()
 {
     while(scanf("%s",str)!=EOF)
     {
         s.clear();
         int n=strlen(str); next[]=,next[]=;
         for(int i=;i<n;i++)
         {
             int j=next[i];
             while(j&&str[i]!=str[j]) j=next[j];
             if(str[i]==str[j]) next[i+]=j+;
             else next[i+]=;
         }
         //for(int i=0;i<=n;i++) printf("%d ",next[i]);
     //    printf("\n");
         int j=strlen(str);
         while(j)
         {
             s.push_back(j);
             j=next[j];
         }
         for(int i=s.size()-;i>=;i--)
         {
             if(i==s.size()-) printf("%d",s[i]);
             else printf(" %d",s[i]);
         }
         printf("\n");
     }
     return ;
 }

poj 2185

题意:输入一个矩阵由字符组成,求出矩阵的最小组成单位。

sol:网上好多代码都是错的,第一次学被误解了,今天重新修改这道题,其实找出每一行的周期串记录下个数,最后等于行数的肯定就是最小的宽。

求高直接公式就好了,

 1 /************************
 2 *    zhuyuqi            *
 3 *    QQ:1113865149      *
 4 *    worfzyq@gmail.com  *
 5 *************************/
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <algorithm>
 9 #include <cmath>
 #include <vector>
 #include <list>
 #include <queue>
 using namespace std;
 const int MAX = 1e4+;
 const int inf = 0x3f3f3f3f;
 char str[MAX][];
 int next[MAX],ML[MAX],vis[MAX];
 int main()
 {
 
     int n,m; int L,R;
     while(scanf("%d %d",&n,&m)==) {
         for(int i=;i<=n;i++) {
             scanf("%s",str[i]+);
         }
        // for(int i=1;i<=n;i++) printf("%s\n",str[i]+1);
         memset(ML,,sizeof(ML));
         if(m>) {
             for(int i=;i<=n;i++) {
                 next[]=; int j=; memset(vis,,sizeof(vis));
                 for(int k=;k<=m;k++) {
                     while(j&&str[i][k]!=str[i][j+]) j=next[j];
                     if(str[i][k]==str[i][j+]) j++;
                     next[k]=j;
                 }
                 int x=m;
             //    for(int k=1;k<=m;k++) printf("%d ",next[k]); printf("\n");
                 while(x) {
                    // if(x==1) break;
                     if(!vis[m-next[x]])
                     ML[m-next[x]]++; x=next[x];  vis[x-next[x]]=;
                 }
             }
             for(int i=;i<=m;i++) if(ML[i]==n) {
                 L=i; break;
             }
         } else L=;
         next[]=; int j=;
         for(int i=;i<=n;i++) {
             while(j&&strcmp(str[i]+,str[j+]+)) j=next[j];
 //            printf("%d %d\n",i,j+1);
             if(!strcmp(str[i]+,str[j+]+)) j++;
           //  printf("%d %d\n",next[i],j);
             next[i]=j;
         }
         //printf("%d %d\n",L,n-next[n]);
         printf("%d\n",(n-next[n])*L);
 
     }
 
     return ;
 }

最新文章

  1. LINQ驱动数据的查询功能
  2. 转载:Solr的自动完成实现方式(第二部分:Suggester方式)
  3. NTP服务配置
  4. 【POJ 1228】Grandpa&#39;s Estate 凸包
  5. Bluetooth GATT介绍
  6. iOS篇之有沙盒缓存
  7. 【CSS3】---样式小技巧
  8. ubuntu配置bridge网桥
  9. nginx添加需要代理的域名 配置
  10. 折腾源WRT的AC路无线路由-2
  11. vue2.0实现前端星星评分功能组件
  12. Day9 基于TCP的套接字和基于UDP的套接字
  13. 集智人工智能学习笔记Python#0
  14. mac抓包工具anyproxy
  15. pandas基础运算
  16. Python列表生成式和生成器
  17. wpgcms---碎片管理的使用
  18. 【2-SAT】【DFS】【分类讨论】Gym - 101617K - Unsatisfying
  19. 由浅入深了解Retrofit(一)
  20. Required String parameter &#39;id&#39; is not present

热门文章

  1. bzoj 1628: [Usaco2007 Demo]City skyline【贪心+单调栈】
  2. bzoj 1629: [Usaco2007 Demo]Cow Acrobats【贪心+排序】
  3. spring 异常处理
  4. [Qt Creator 快速入门] 第0篇 开始学习Qt 与Qt Creator
  5. C#时间相关方法
  6. jQuery学习笔记(3)-操作jQuery包装集的函数
  7. js一键导出Excel
  8. win7如何设置自动关机
  9. Android显示相册图片和相机拍照
  10. MySQL的基本概念与操作