UVa 1401 Remember the word

Posted 大四开始ACM

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa 1401 Remember the word相关的知识,希望对你有一定的参考价值。

方法:dp + trie

比较明显可以想出一个dp,对于给定的string str,d[i] = 表示str.substr(i) 的方法数, d[str.length()] = 1, d[i] = sum(d[i+x.length()], x 是 str.substr(i) 的prefix),最后答案是d[0]。状态转移的时候,可以通过走由可行x组成的trie来判断。

code:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <string>
  6 #include <vector>
  7 #include <stack>
  8 #include <bitset>
  9 #include <cstdlib>
 10 #include <cmath>
 11 #include <set>
 12 #include <list>
 13 #include <deque>
 14 #include <map>
 15 #include <queue>
 16 #include <fstream>
 17 #include <cassert>
 18 #include <unordered_map>
 19 #include <unordered_set>
 20 #include <cmath>
 21 #include <sstream>
 22 #include <time.h>
 23 #include <complex>
 24 #include <iomanip>
 25 #define Max(a,b) ((a)>(b)?(a):(b))
 26 #define Min(a,b) ((a)<(b)?(a):(b))
 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
 32 #define FOREACH(a,b) for (auto &(a) : (b))
 33 #define rep(i,n) FOR(i,0,n)
 34 #define repn(i,n) FORN(i,1,n)
 35 #define drep(i,n) DFOR(i,n-1,0)
 36 #define drepn(i,n) DFOR(i,n,1)
 37 #define MAX(a,b) a = Max(a,b)
 38 #define MIN(a,b) a = Min(a,b)
 39 #define SQR(x) ((LL)(x) * (x))
 40 #define Reset(a,b) memset(a,b,sizeof(a))
 41 #define fi first
 42 #define se second
 43 #define mp make_pair
 44 #define pb push_back
 45 #define all(v) v.begin(),v.end()
 46 #define ALLA(arr,sz) arr,arr+sz
 47 #define SIZE(v) (int)v.size()
 48 #define SORT(v) sort(all(v))
 49 #define REVERSE(v) reverse(ALL(v))
 50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
 52 #define PERMUTE next_permutation
 53 #define TC(t) while(t--)
 54 #define forever for(;;)
 55 #define PINF 1000000000000
 56 #define newline \'\\n\'
 57 
 58 #define test if(1)if(0)cerr
 59 using namespace std;
 60 using namespace std;
 61 typedef vector<int> vi;
 62 typedef vector<vi> vvi;
 63 typedef pair<int,int> ii;
 64 typedef pair<double,double> dd;
 65 typedef pair<char,char> cc;
 66 typedef vector<ii> vii;
 67 typedef long long ll;
 68 typedef unsigned long long ull;
 69 typedef pair<ll, ll> l4;
 70 const double pi = acos(-1.0);
 71 
 72 const int maxnode = 400000+5;
 73 const int sigma_size = 26;
 74 
 75 
 76 struct Trie
 77 {
 78     int g[maxnode][sigma_size];
 79     int val[maxnode];
 80     int sz;
 81     int newnode()
 82     {
 83         Reset(g[sz], 0);
 84         val[sz] = 0;
 85         return sz++;
 86     }
 87     void init() { sz = 1; Reset(g[0], 0); };
 88     int idx(char c) { return c-\'a\'; };
 89 
 90     void insert(const string &str, int v)
 91     {
 92         int u = 0, n = str.length();
 93         for (int i = 0; i < n; ++i)
 94         {
 95             int c = idx(str[i]);
 96             if (!g[u][c]) g[u][c] = newnode();
 97             u = g[u][c];
 98         }
 99         val[u] = v;
100     }
101 };
102 
103 Trie solver;
104 string str;
105 string input[4000];
106 int n;
107 const int mod = 20071027;
108 int d[300001];
109 int main()
110 {
111     ios::sync_with_stdio(false);
112     cin.tie(0);
113     int kase = 0;
114     while (cin >> str >> n)
115     {
116         solver.init();
117         for (int i = 0; i < n; ++i) cin >> input[i];
118         for (int i = 0; i < n; ++i) solver.insert(input[i], 1);
119         int len = str.length();
120         Reset(d, 0);
121         d[len] = 1;
122         for (int i = len-1; i >= 0; --i)
123         {
124             int u = 0;
125             for (int j = i; j < len; ++j)
126             {
127                 int c = solver.idx(str[j]);
128                 if (!solver.g[u][c])
129                 {
130                     break;
131                 }
132                 u = solver.g[u][c];
133                 if (solver.val[u]) d[i] = (d[i] + d[j+1]) % mod;
134             }
135         }
136         cout << "Case " << ++kase << ": " << d[0] << newline;
137     }
138 }
View Code

 

以上是关于UVa 1401 Remember the word的主要内容,如果未能解决你的问题,请参考以下文章

[UVA]UVA1401 remember the name(Trie)

UVA1401Remember the Word Trie+dp

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

UVA-3942 Remember the Word

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

UVA3942 Remember the Word