zoukankan      html  css  js  c++  java
  • [HDU 3336] Count the string

    题目传送-HDU3336

    题意:

    给你一个长度为(n)的字符串(S),问这个字符串的所有前缀在字符串中出现的次数之和(mod (10007))
    (nle 200000)

    题解:

    1.考虑KMP,并记录(f_i)表示以(i)为结尾的所有串中是前缀的个数,那么答案显然是所有(f)之和
    根据KMP中nxt数组的定义,(f_i=f_{nxt_i}+1)(如果这里不懂的自行搜索KMP理解)
    2.不用动脑子的后缀自动机.

    过程:

    顺利AC

    代码:

    const int LEN=200010;
    int n,nxt[LEN];
    char s[LEN];
    int f[LEN];
    signed main() {
    	int T; read(T);
    	while(T--) {
    		read(n); scanf("%s",s+1);
    		int j=0;
    		for(int i=2;i<=n;i++) {
    			while(s[j+1]!=s[i] && j) j=nxt[j];
    			if(s[j+1]==s[i]) ++j;
    			nxt[i]=j;
    		}
    		for(int i=1;i<=n;i++)
    			f[i]=f[nxt[i]]+1;
    		ll ans=0;
    		for(int i=1;i<=n;i++)
    			(ans+=1ll*f[i])%=10007;
    		printf("%lld
    ",ans);
    	}
    
    	return 0;
    }
    /*
    2
    4
    abab
    4
    abab
    */
    

    用时:15min

  • 相关阅读:
    shell脚本进阶
    sort与uniq命令
    sed命令
    DNS与CDN
    nginx
    Docker Private Registry
    docker存储卷
    docker容器网络配置
    docker容器网络
    docker容器虚拟化
  • 原文地址:https://www.cnblogs.com/functionendless/p/9469972.html
Copyright © 2011-2022 走看看