• la 3942 Trie数应用


    Remember the Word  Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu 
    Submit Status
    Appoint description:

    Description

    Download as PDF

    Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing 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$ \le$S$ \le$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.

    Sample Input

    abcd 
    4 
    a 
    b 
    cd 
    ab
    

    Sample Output

    Case 1: 2
    

    // Trie 树应用
    #include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <ctype.h> #include <stack> #include <iostream> #define sigma_size 26 using namespace std; const int Max =4000*100+10; int ch[Max][sigma_size]; int val[Max]; int cnt; int idx(int c){return c-'a';} void insert(char *s) { int i,n=strlen(s); int c,u=0; for(i=0;i<n;i++) { c=idx(s[i]); if(!ch[u][c]) { memset(ch[cnt],0,sizeof(ch[cnt])); val[cnt]=0; ch[u][c]=cnt++; } u=ch[u][c]; } val[u]=n; } char str[300010]; int d[300010]; void search(char *s,int i,int len) { int j; int c,u=0; for(j=0;j<len;j++) { c=idx(s[j]); if(!ch[u][c]) break; u=ch[u][c]; if(val[u]){ d[i]=(d[i]+d[ i+val[u] ])%20071027; } } } int main() { // freopen("in.txt","r",stdin); int num=1; int S,n; int i; char s[110]; while(scanf("%s",str)!=EOF) { cnt=1;val[0]=0; memset(ch[0],0,sizeof(ch[0])); n=strlen(str); scanf("%d",&S); while(S--) { scanf("%s",s); insert(s); } memset(d, 0, sizeof(d)); d[n]=1; for(i=n-1;i>=0;i--) { search(str+i,i,n-i); } printf("Case %d: %d\n",num++,d[0]); } return 0; }
  • 相关阅读:
    docker 部署springcloud项目【限制容器内存、CPU】
    docker容器中安装vim 、telnet、ifconfig、ping、curl命令
    开源仓库Harbor搭建及配置过程
    动手学深度学习 | PyTorch神经网络基础 | 14
    动手学深度学习 | 实战:Kaggle房价预测+课程竞赛:加州2020年房价预测 | 13
    动手学深度学习 | 数值稳定性+模型初始化和激活函数 | 12
    动手学深度学习 | 丢弃法 | 11
    动手学深度学习 | 模型选择+过拟合和欠拟合 | 09
    动手学深度学习 | 多层感知机+代码实现 | 08
    动手学深度学习 | Softmax回归+损失函数+图片分类数据集 | 07
  • 原文地址:https://www.cnblogs.com/372465774y/p/3011494.html
Copyright © 2020-2023  润新知