UVa 1401 Remember the Word

Trie+DP

大白书上的字典树训练题。

题意是说一个字符串可能有多少种小串组成。

例如

abcd

4

a

b

cd

ab

abcd=a+b+cd;abcd=ab+cd;

递推为:从最后一位往前,dp[i]=dp[i]+dp[i+ len[x]]  x为输入时的顺序,附加到节点中。是 i~strlen(S)的前缀。S[1,2,3,…,i,…len]

构建Trie树时,把顺序也附加到节点中。

最后search的时候 找出为多少个字符串的前缀,并把这些前缀都加起来。

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
#define LL long long
using namespace std;
const int MOD=20071027;

struct Trie
{
    int word[4001*100][26];
    int ex[4001*100];
    int size;

    void clear()
    {
        memset(word[0],0,sizeof(word[0]));
        size=1;
        ex[0]=0;
    }

    int insert(char *s,int v)
    {
        int u=0,c;
        for(int i=0; s[i]!='\0'; i++)
        {
            int c=s[i]-'a';
            if(!word[u][c])
            {
                memset(word[size],0,sizeof(word[size]));
                ex[size]=0;
                word[u][c]=size++;
            }
            u=word[u][c];
        }
        ex[u]=v;
    }

    int search(char *s,int len,vector<int>& ans)
    {
        int u=0,c;
        for(int i=0; s[i]!='\0'&&i<len; i++)
        {
            c=s[i]-'a';
            if(!word[u][c])return 0;
            u=word[u][c];
            if(ex[u])
                ans.push_back(ex[u]);
        }
    }
} wo;

char str[300001];
int dp[300001];
int n;
int le[4001];

int main()
{
    int nn=1;
    while(scanf("%s",str)!=EOF)
    {
        scanf("%d",&n);
        wo.clear();
        memset(dp,0,sizeof(dp));
        char tmp[101];
        int len=strlen(str);

        for(int i=0; i<n; i++)
        {
            scanf("%s",tmp);
            le[i+1]=strlen(tmp);
            wo.insert(tmp,i+1);
        }
        dp[len]=1;
        for(int i=len-1; i>=0; i--)
        {
            vector<int>ans;
            wo.search(str+i,len-i,ans);
            for(int j=0; j<ans.size(); j++)
                dp[i]=(dp[i]+dp[i+le[ans[j]]])%MOD;
        }
        printf("Case %d: %d\n",nn++,dp[0]);
    }
}

UVa 1401 Remember the Word,布布扣,bubuko.com

时间: 2024-08-25 03:51:23

UVa 1401 Remember the Word的相关文章

UVA - 1401 Remember the Word(trie+dp)

1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树,然后从后向前dp,dp[i]=dp[i]+dp[i+len(x)]; 其中x为当前找到的前缀长度. 3. #include<iostream> #include<stdio.h> #include<string.h> using namespace std; #defin

uva 1401 Remember the Word ( Trie + DP )

题意:给出n(n<=4000)个单词和一个字符串(len<=300000),求把这个字符串分解成若干个单词的拼接,有多少种方法 思路:Trie,先把单词建成Trie,然后进行dp,dp[i]表示以字符串中第i个字母为开头的情况,然后每个状态只要在Trie树上找到相应的字母开头的单词,然后dp[i] = sum{dp[i + len(x)]}进行状态转移即可 这个x是n个单词中的一个,可以用Trie查询 code: #include<cstdio> #include<cstri

LA 3942 &amp;&amp; UVa 1401 Remember the Word (Trie + DP)

题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚开始交一直是runtime error,找了好久,一直以为是数组开小了,不断增大还是这样,后来发现我用了char类型...下面分析这个题目 应该不难想到这个状态转移方程: d(i) = sum{d(i+len(x))|单词x是s[i...L]的前缀},其中len(x)是长度.d(i)表示从字符i开始

uva 1401

Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowingthat 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 sti

uva 1401 dp+Trie

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=4147 题意:给定一个字符串,以及若干单词,求有几种方式能用单词组成字符串 我先是dp方程推得有问题不知怎么修改搞得卡了很久,然后就是数组开得太小一直RE trie数组大小=单词个数*单词长度  dp[i]为以str[i]开头的后缀的ans,dp[i]=segma(

UVa 1401 (Tire树) Remember the Word

d(i)表示从i开始的后缀即S[i, L-1]的分解方法数,字符串为S[0, L-1] 则有d(i) = sum{ d(i+len(x)) | 单词x是S[i, L-1]的前缀 } 递推边界为d(L) = 1,代表空串. 将前n个单词构造一颗Tire树,在树中查找后缀的过程中遇到一个单词节点就代表找到一个状态转移中的x 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxnode = 400000 + 10; 5 co

UVA 3942 - Remember the Word (Trie)

3942 - 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 number

UVA 3942 -- Remember the Word (字典树+dp)

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 clear

uva 1401 Fast Matrix Operations 快速矩阵操作 (线段树 区间修改和查询)

题意:给一个r行c列的全0矩阵,支持以下三种操作: 1 x1 y1 x2 y2 v 子矩阵(x1 y1 x2 y2)的所有元素增加v 2 x1 y1 x2 y2 v 子矩阵(x1 y1 x2 y2)的所有元素设为v 3 x1 y1 x2 y2   查询子矩阵(x1 y1 x2 y2)的元素和.最小值.最大值. 子矩阵(x1 y1 x2 y2)是指满足 x1 <= x <= x2, y1 <= y <= y2的所有元素(x,y). 矩阵不超过20行,矩阵总元素可多达10^6个. 思路