zoukankan      html  css  js  c++  java
  • Remember the Word

    Remember the Word

    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 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.

    Sample Input

    abcd 
    4 
    a 
    b 
    cd 
    ab

    Sample Output

    Case 1: 2
    分析:trie+dp;
       转移方程为dp[i]=dp[i]+dp[i+len[x]];
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <unordered_map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 20071027
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    const int maxn=4e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,ch[26][maxn],sz,cas,lent[4010],num[maxn];
    char str[maxn],node[4010][110];
    vi tmp;
    ll dp[maxn];
    void insert(char *p,int len,int id)
    {
        int now=0;
        for(int i=0;i<len;i++)
        {
            int x=p[i]-'a';
            if(!ch[x][now])ch[x][now]=++sz;
            now=ch[x][now];
        }
        num[now]=id;
    }
    void gao(int p)
    {
        int now=0;
        for(int i=p;str[i];i++)
        {
            int x=str[i]-'a';
            if(num[now])dp[p]=(dp[p]+dp[p+lent[num[now]]])%mod;
            if(!ch[x][now])return;
            now=ch[x][now];
        }
        if(num[now])dp[p]=(dp[p]+dp[p+lent[num[now]]])%mod;
    }
    int main()
    {
        int i,j;
        while(~scanf("%s",str))
        {
            int len=strlen(str);
            memset(dp,0,sizeof(dp));
            memset(num,0,sizeof(num));
            memset(ch,0,sizeof(ch));
            dp[len]=1;
            sz=0;
            scanf("%d",&n);
            rep(i,1,n){
                scanf("%s",node[i]);
                lent[i]=strlen(node[i]);
                insert(node[i],lent[i],i);
            }
            for(i=len-1;i>=0;i--)gao(i);
            printf("Case %d: %lld
    ",++cas,dp[0]);
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    Linux常用命令
    全文搜索服务器solr
    非关系型数据库之redis
    springMVC流程
    浅谈spring框架的控制反转和依赖注入
    Java基础之数组
    Java基础之方法
    跨域访问接口,传递参数
    Centos 6无法使用yum
    内网穿透工具:钉钉HTTP内网穿透使用与讲解
  • 原文地址:https://www.cnblogs.com/dyzll/p/5995193.html
Copyright © 2011-2022 走看看