您的位置:首页 > 其它

【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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: