zoukankan      html  css  js  c++  java
  • Luogu P2375 [NOI2014]动物园


    Luogu P2375 [NOI2014]动物园

    解析

    • KMP 的神奇应用

    Code

    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define LL long long
    using namespace std;
    const int N=1e6+5;
    const LL mod=1e9+7;
    int n,p[N];
    char ch[N];
    LL ans,num[N];
    LL kmp(char s[])
    {
    	int j=0,len=strlen(s+1);
    	num[1]=1;
    	for(int i=1;i<len;i++)
    	{
    		while(j!=0&&s[i+1]!=s[j+1]) j=p[j];
    		if(s[i+1]==s[j+1]) j++;
    		p[i+1]=j;
    		num[i+1]=num[p[i+1]]+1;
    	}
    	j=0;
    	LL res=1;
    	for(int i=1;i<len;i++)
    	{
    		while(j!=0&&s[i+1]!=s[j+1]) j=p[j];
    		if(s[i+1]==s[j+1]) j++;
    		while(j>((i+1)>>1)) j=p[j];
    		res=(res*(num[j]+1))%mod;
    	}
    	return res;
    }
    int main()
    {
    	scanf("%d",&n);
    	while(n--)
    	{
    		cin>>(ch+1);
    		ans=kmp(ch);
    		printf("%lld
    ",ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    jquery index与eq
    尝试一下
    document
    2017-03-28 java script DOM操作
    2017-03-25 CSS 样式
    CSS 样式表分类
    CSS 样式表
    HTML 框架
    表格
    HTML常用标记
  • 原文地址:https://www.cnblogs.com/Hawking-llfz/p/11511037.html
Copyright © 2011-2022 走看看