zoukankan      html  css  js  c++  java
  • BZOJ5337: [TJOI2018]str

    BZOJ5337: [TJOI2018]str

    https://lydsy.com/JudgeOnline/problem.php?id=5337

    分析:

    • 这题数据范围实在不明确,我以为总串长是(10^7)级别的,一直在想一个和总串长相关的线性做法...然后膜了题解。
    • 这玩意,设(f[i][j])表示匹配前(i)个串,当前已经匹配到(s)的第(j)个字符上的方案数,转移只需要用哈希判断两串相等就行了。

    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cstdlib>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    #define base 10010257
    #define N 10050
    #define mod 1000000007
    ull mi[N],h[N];
    char w[N],s[N];
    int K,n;
    ll f[105][N];
    inline void upd(ll &x,ll y) {
    	x=x+y; if(x>=mod) x-=mod;
    }
    ull geth(int l,int r) {
    	return h[r]-h[l-1]*mi[r-l+1];
    }
    int main() {
    	scanf("%d%s",&K,w+1);
    	n=strlen(w+1);
    	int i,j;
    	for(mi[0]=i=1;i<=n;i++) mi[i]=mi[i-1]*base;
    	for(i=1;i<=n;i++) h[i]=h[i-1]*base+w[i];
    	for(i=0;i<n;i++) f[0][i]=1;
    	for(i=0;i<K;i++) {
    		int x;
    		scanf("%d",&x);
    		while(x--) {
    			scanf("%s",s+1);
    			int m=strlen(s+1);
    			ull nh=0;
    			for(j=1;j<=m;j++) nh=nh*base+s[j];
    			for(j=0;j<=n-m;j++) if(f[i][j]) {
    				if(geth(j+1,j+m)==nh) upd(f[i+1][j+m],f[i][j]);
    			}
    		}
    	}
    	ll ans=0;
    	for(i=1;i<=n;i++) upd(ans,f[K][i]);
    	printf("%lld
    ",ans);
    }
    
  • 相关阅读:
    JS高级
    函数作用域面试题
    11.14
    11.13
    Redux知识
    react-router-dom
    react 的三大属性
    vuex
    数组的扩展
    函数作用域和 class
  • 原文地址:https://www.cnblogs.com/suika/p/10229821.html
Copyright © 2011-2022 走看看