zoukankan      html  css  js  c++  java
  • 【HNOI 2004】 L语言

    【题目链接】

               https://www.lydsy.com/JudgeOnline/problem.php?id=1212

    【算法】

                字典树 + dp

     【代码】

               

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXL 10000010
    #pragma GOC optimize("O2")
    
    int i,j,n,m,len,ans,mx;
    char s[MAXL];
    bool ok[MAXL];
    
    struct Trie
    {
            int tot;
            struct Node
            {
                    int child[26];
                    bool is_last;    
            }    a[310];
            inline void insert(char *s)
            {
                    int i,len,x = 0;
                    len = strlen(s);
                    for (i = 0; i < strlen(s); i++)
                    {
                            if (!a[x].child[s[i]-'a']) a[x].child[s[i]-'a'] = ++tot;
                            x = a[x].child[s[i]-'a'];        
                    }
                    a[x].is_last = true;
            }
            inline bool check(int l,int r)
            {
                    int i,x = 0;
                    for (i = l; i <= r; i++)
                    {
                            if (a[x].child[s[i]-'a']) x = a[x].child[s[i]-'a'];
                            else return false;
                    }
                    return a[x].is_last;
            }
    } T;
    
    int main() 
    {
            
            scanf("%d%d",&n,&m);
            for (i = 1; i <= n; i++)
            {
                    scanf("%s",&s);
                    mx = max(mx,(int)strlen(s));
                    T.insert(s);        
            }
            while (m--)
            {
                    ans = 0;
                    memset(ok,false,sizeof(ok));
                    scanf("%s",&s);
                    len = strlen(s);
                    for (i = 0; i < len; i++)
                    {
                            for (j = max(i-mx,-1); j < i; j++)
                            {
                                    if ((j == -1 || ok[j]) && T.check(j+1,i))
                                    {
                                            ok[i] = true;
                                            ans = i + 1;
                                            break;
                                    }
                            }
                    }        
                    printf("%d
    ",ans);
            }
            
            return 0;
        
    }
  • 相关阅读:
    算法
    日常
    算法
    算法
    算法
    算法
    NaviCat连接mysql出现加密方式错误的解决方案:
    Sql sugar的使用
    报表体联查详情页面
    第一次用临时表的感受:
  • 原文地址:https://www.cnblogs.com/evenbao/p/9257965.html
Copyright © 2011-2022 走看看