UVa 1401 Remember The Word

Source: Internet
Author: User

Trie+dp


Terry Petition training on the problem.

It says that Italy is a string that can have a number of small strings composed.

For example

Abcd

4

A

B

Cd

Ab

ABCD=A+B+CD. ABCD=AB+CD;


Recursion is: From the last one forward. dp[i]=dp[i]+dp[i+ Len[x]] x is appended to the node in the order it was entered. is the prefix for I~strlen (S). s[1,2,3,..., i,... Len]

When building the trie tree, the order is also appended to the node.


At the end of the search, find out how many strings are prefixed and add up the prefixes.



#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 longusing 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]!= '; 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]!= ' &&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]); }}


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

UVa 1401 Remember The Word

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.