zoukankan      html  css  js  c++  java
  • Codeforces 633 C Spy Syndrome 2 字典树

    题意:还是比较好理解

    分析:把每个单词反转,建字典树,然后暴力匹配加密串

    注:然后我就是特别不理解,上面那种能过,而且时间很短,但是我想反之亦然啊

    我一开始写的是,把加密串进行反转,然后单词正着建字典树,然后就TLE了,反着写就能过

    真是百思不得解,然后我猜测可能是单词数目比较少

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <vector>
    #include <string>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int N=1000000+5;
    char s[10005];
    char str[100005][1005];
    int len[100005];
    int trie[N][26],mk[N];
    int ans[10005],tot,cnt,m,n;
    void add(int x)
    {
        int now=0;
        for(int i=len[x]-1; i>=0; --i)
        {
            char tmp=str[x][i];
            if(tmp<'a'||tmp>'z')
                tmp+='a'-'A';
            int p=tmp-'a';
            if(!trie[now][p])
                trie[now][p]=++cnt;
            now=trie[now][p];
        }
        mk[now]=x;
    }
    void solve(int pos)
    {
        if(pos==n)
        {
            for(int i=1; i<tot; ++i)
                printf("%s ",str[ans[i]]);
            printf("%s
    ",str[ans[tot]]);
           exit(0);
        }
        int now=0;
        for(int i=pos+1; i<=n; ++i)
        {
            int p=s[i]-'a';
            now=trie[now][p];
            if(!now)break;
            if(mk[now])
            {
                ans[++tot]=mk[now];
                solve(pos+len[mk[now]]);
                --tot;
            }
        }
    }
    int main()
    {
        scanf("%d%s",&n,s+1);
    //    for(int i=1; i<=n/2; ++i)
    //        swap(s[i],s[n-i+1]);
        scanf("%d",&m);
        for(int i=1; i<=m; ++i)
        {
            scanf("%s",str[i]);
            len[i]=strlen(str[i]);
            add(i);
        }
        solve(0);
        return 0;
    }
    View Code
  • 相关阅读:
    实验室网络管理记
    mondrian schema一个简单的例子
    powerdesigner数据库设计导入oracle9i
    linux 的mount命令
    [转]moto面试题
    Oracle数据库服务解释
    数据聚集技术在mondrian中的实现
    发现台湾的mondrian资料 + pentaho截图
    一个封装比较完整的FTP类(转载)
    oracle 网页管理工具登录接口
  • 原文地址:https://www.cnblogs.com/shuguangzw/p/5223547.html
Copyright © 2011-2022 走看看