【BZOJ 1212】[HNOI2004]L语言
2018-03-19 20:56
417 查看
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
因为查询的字典里面。单词的最大长度为10
所以。
如果建立一棵字典树的话。
深度最多为10;
那么可以写一个DP;
设f[i]表示1..i这一段是否能被理解
f[0] = 1;
然后从从第i+1个位置开始扫描。
沿着树根往下走就好了。
(假设当前扫描的长度为len
遇到单词。
那么f[i+len]置为true.
在每个f[i]=1的位置都进行一次字典树的遍历就好。
1M=10^6
所以10^7的复杂度。
够了
【代码】
#include <bits/stdc++.h> #define LL long long #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) #define all(x) x.begin(),x.end() #define pb push_back #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 using namespace std; const double pi = acos(-1); const int dx[4] = {0,0,1,-1}; const int dy[4] = {1,-1,0,0}; const int L = 10; const int N = 26; const int M = 1e6; struct abc{ int cnt; abc *nex[N+10]; }; int n,m,f[M+10]; abc *root; char s[L+10],S[M+10]; abc *New_Node(){ abc *temp = new abc; temp->cnt = 0; for (int i = 0;i < 26;i++) temp->nex[i] = NULL; return temp; } int main(){ #ifdef LOCAL_DEFINE freopen("rush_in.txt", "r", stdin); #endif root = New_Node(); scanf("%d%d",&n,&m); rep1(i,1,n){ scanf("%s",s); int len = strlen(s); abc *temp = root; for (int i = 0;i < len;i++){ if (temp->nex[s[i]-'a']==NULL){ temp->nex[s[i]-'a'] = New_Node(); } temp = temp->nex[s[i]-'a']; } temp->cnt++; } rep1(i,1,m){ scanf("%s",S+1); int len = strlen(S+1); f[0] = i; for (int j = 0;j < len;j++) if (f[j]==i){ abc *temp = root; rep1(k,j+1,j+10){ if (k>len) break; if (temp->nex[S[k]-'a']==NULL){ break; } temp = temp->nex[S[k]-'a']; if (temp->cnt>0) f[k] = i; } } int ans = 0; rep1(j,1,len) if (f[j]==i) ans = j; printf("%d\n",ans); } return 0; }
相关文章推荐
- [bzoj1212] [HNOI2004]L语言(trie+dp)
- BZOJ_1212_[HNOI2004]L语言_哈希
- Bzoj1212: [HNOI2004]L语言
- BZOJ1212 [HNOI2004]L语言
- bzoj1212: [HNOI2004]L语言
- BZOJ1212 [HNOI2004] L语言
- [BZOJ1212][HNOI2004]L语言(AC自动机+dp)
- bzoj 1212: [HNOI2004]L语言
- BZOJ 1212 HNOI 2004 L语言 Trie树
- bzoj 1212: [HNOI2004]L语言
- BZOJ 1212 HNOI2004 L语言 AC自动机(Trie树)+动态规划
- [bzoj1212][HNOI2004]L语言_AC自动机_动态规划
- 【BZOJ1212】[HNOI2004]L语言 Trie树
- BZOJ1212: [HNOI2004]L语言 AC自动机
- [省选前题目整理][BZOJ 1212][HNOI 2004]L语言(Trie)
- 【bzoj1212】[HNOI2004]L语言 AC自动机
- BZOJ 1212: [HNOI2004]L语言( dp + trie )
- 【BZOJ1212】[HNOI2004]L语言【Trie】【暴力】
- [HNOI2004][bzoj1212] L语言 [Trie+dp]
- [DP+AC自动机] BZOJ1212: [HNOI2004]L语言