UVALive 3942 Remember the Word 字典树+dp

/**
题目:UVALive 3942 Remember the Word
链接:https://vjudge.net/problem/UVALive-3942
题意:给定一个字符串(长度最多3e5)和m个单词(每个单词长度最多100)。单词都是不同的。该字符串可以由若干个单词组成,问最多有多少种组合方式。

思路:字典树+dp
用字典树处理好m个单词,定义dp[i]表示从i开始的字符串可以由单词组成的方式数。
那么dp[i] += dp[i+j]; j表示某个单词和字符串的[i,i+j-1]匹配。

*/

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int mod = 20071027;
const int INF = 0x3f3f3f3f;
const int maxnode = 4e5+10;///最多可能有多少个节点
const int maxn = 3e5+100;
const int sigma_size = 26;///26个字母
int dp[maxn];
char s[maxn], ss[maxn];安全1
int ch[maxnode][sigma_size];///由于很大,所以结构体内部放不下。要放在外面。
struct Trie{
    int val[maxnode];
    int sz;
    int idx(char c){return c-‘a‘;}

    void insert(char *s,int v)
    {
        int u = 0, n = strlen(s);
        for(int i = 0; i < n; i++){
            int c = idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz], 0, sizeof ch[sz]);
                val[sz] = 0;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
        }
        val[u] = v;
    }

    void query(int sta,int off)
    {
        int u = 0;
        int c = idx(s[sta]);
        while(s[sta]!=‘\0‘&&ch[u][c]){
            if(val[ch[u][c]]){
                dp[sta-off] = (dp[sta-off]+dp[sta+1])%mod;
            }
            u = ch[u][c];
            c = idx(s[++sta]);
            off++;
        }
    }
};

int main()
{
    int cas = 1;
    int n;
    Trie trie;
    while(scanf("%s",s)==1)
    {
        scanf("%d",&n);
        trie.sz = 1;
        memset(ch[0], 0, sizeof ch[0]);
        for(int i = 0; i < n; i++){
            scanf("%s",ss);
            trie.insert(ss,1);
        }
        int m = strlen(s);
        memset(dp, 0, sizeof dp);
        dp[m] = 1;
        for(int i = m-1; i>=0; i--){
            trie.query(i,0);
        }
        printf("Case %d: %d\n",cas++,dp[0]);
    }
    return 0;
}
时间: 2024-11-12 17:43:52

UVALive 3942 Remember the Word 字典树+dp的相关文章

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

LA 3942 Remember the Word 字典树+dp

#include <cstdio> #include <cstring> using namespace std; #define mod 20071027 int dic[401000][28],val[401000]; char str[301000]; int dp[301000]; int s,sz; char T[110]; void insert(char *ch) { int u=0,len=strlen(ch); for(int i=0;i<len;i++)

UVALive 3942 - Remember the Word(DP,数组Trie+指针Trie)

UVALive 3942 - Remember the Word(DP,数组Trie+指针Trie) ACM 题目地址: UVALive 3942 - Remember the Word 题意: 给一些单词,然后给一个长的单词,问有几种方法能组成这个大单词,单词可以重复用. 分析: DP[i]=sum{DP[j} (i<j<len),从后往前求. 本来用数组Trie写得爽爽的,1A了. 发现2s多,不能忍! 然后用指针Trie写了一遍,各种出错,整个人都不好了... 研究了一遍别人代码,发现快

UVALive - 3942 Remember the Word[Trie DP]

UVALive - 3942 Remember the Word 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 remem

UVALive - 3942 - Remember the Word (Trie树)

UVALive - 3942 Remember the Word Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a p

UVALive - 3942 Remember the Word[树状数组]

UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device with a variable electric resistance. It has two terminals and some kind of control mechanism (often a dial, a wheel or a slide) with which the resistance

2010辽宁省赛 NBUT 1222 English Game【字典树+DP】

[1222] English Game 时间限制: 1000 ms 内存限制: 131072 K 链接:Click Here! 问题描述 This English game is a simple English words connection game. The rules are as follows: there are N English words in a dictionary, and every word has its own weight v. There is a wei

【暑假】[实用数据结构]UVAlive 3942 Remember the Word

UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Neal is very curious about combinatorial problems, and now here comes a problem about words. Know

zoj3013Word Segmenting (字典树+dp)

Word Segmenting Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge One key technology in Chinese search engine is Word Segmenting, which is more difficult than English Word Segmenting, as there is no space between words. A traditional solutio