zoukankan      html  css  js  c++  java
  • UVALive

    UVALive - 3942

    Neal is very curious about combinatorial problems, and now here comes a problem about words. Know- ing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie.

    Since Jiejie can’t remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie’s only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks.

    The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of ways the given word can be divided, using the words in the set.

    Input

    The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000.

    The second line contains an integer S, 1 ≤ S ≤ 4000.

    Each of the following S lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase.

    There is a blank line between consecutive test cases. You should proceed to the end of file.

    Output

    For each test case, output the number, as described above, from the task description modulo 20071027.


    白书,字符串用单词分割


    DP,d[i]表示i到结尾的方案数

    d[i]=sum{d[i+len(x)]|x是s[i]开始的前缀}

    暴力的话枚举每个单词代价4000

    可以用Trie找前缀代价100

    //
    //  main.cpp
    //  la3942
    //
    //  Created by Candy on 10/14/16.
    //  Copyright © 2016 Candy. All rights reserved.
    //
    
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int W=3e5+5,S=4e3+5,N=105,MOD=20071027;
    int n;
    char s[W];
    char a[S][N];
    int t[S*N][30],sz=0,val[S*N];
    void init(){
        memset(t[0],0,sizeof(t[0]));
    }
    void insert(char ch[]){
        int len=strlen(ch+1),u=0;
        for(int i=1;i<=len;i++){
            int c=ch[i]-'a';
            if(!t[u][c]){
                t[u][c]=++sz;
                memset(t[sz],0,sizeof(t[sz]));
                val[sz]=0;
            }
            u=t[u][c];
        }
        val[u]=1;
    }
    int d[W];
    void dp(){
        memset(d,0,sizeof(d));
        int n=strlen(s+1);
        d[n+1]=1;
        for(int i=n;i>=1;i--){
            int u=0;
            for(int j=i;j<=n&&j<=i+100;j++){
                int c=s[j]-'a';
                if(!t[u][c]) break;
                u=t[u][c];
                if(val[u]) d[i]=(d[i]+d[j+1])%MOD;
            }
        }
    }
    int main(int argc, const char * argv[]) {
        int cas=0;
        while(scanf("%s%d",s+1,&n)!=EOF){
            init();
            for(int i=1;i<=n;i++){
                scanf("%s",a[i]+1);
                insert(a[i]);
            }
            dp();
            printf("Case %d: %d
    ",++cas,d[1]);
        }
        return 0;
    }
  • 相关阅读:
    扩展知识
    day61——多表操作(增、删除、改、基于对象的跨表查询)
    day60——单表操作补充(批量插入、查询、表结构)
    day59——orm单表操作
    day58——模板继承、组件、自定义标签和过滤器、inclusion_tag、静态文件配置、url别名和反向解析、url命名空间
    day57——视图、模板渲染
    WARNING: Ignoring invalid distribution -ip
    Python- 【python无法更新pip】提示python.exe: No module named pip
    Anaconda Prompt 切换路径不能进入D盘
    Failed calling sys.__interactivehook__ 错误的解决
  • 原文地址:https://www.cnblogs.com/candy99/p/5959943.html
Copyright © 2011-2022 走看看