【Trie】bzoj1212 [HNOI2004]L语言
生活随笔
收集整理的這篇文章主要介紹了
【Trie】bzoj1212 [HNOI2004]L语言
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
枚舉每個文章里已經在Trie中被標記為可能是分割處的字符,然后再從此處跑Trie,繼續向后標記。由于單詞數很少,因此復雜度可以接受,O(n*m*Len)。
#include<cstdio> #include<cstring> using namespace std; int n,m,L; char s[1024*1024+100]; int ch[21*11][26]; bool is[21*11],end[1024*1024+100]; int sz; void Insert(char s[]) {int U=0,len=strlen(s);for(int i=0;i<len;++i){int V=s[i]-'a';if(!ch[U][V]) ch[U][V]=++sz;U=ch[U][V];}is[U]=1; } void work(int x) {int i=x,j=ch[0][s[x]-'a'];while(i<L&&j){if(is[j]) end[i]=1;++i; j=ch[j][s[i]-'a'];} } int main() {scanf("%d%d",&n,&m);for(int i=1;i<=n;++i) {scanf("%s",s); Insert(s);}for(;m;--m){scanf("%s",s); L=strlen(s); memset(end,0,L*sizeof(bool));work(0);for(int i=0;i<L-1;++i) if(end[i]) work(i+1);for(int i=L-1;i>=0;--i) if(end[i]) {printf("%d\n",i+1); goto OUT;}puts("0");OUT:;}return 0; }轉載于:https://www.cnblogs.com/autsky-jadek/p/4309113.html
總結
以上是生活随笔為你收集整理的【Trie】bzoj1212 [HNOI2004]L语言的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两篇很牛的vim使用技巧
- 下一篇: 揭秘富人的22种习惯与风格