zoukankan      html  css  js  c++  java
  • Games on a CD

    Games on a CD
    time limit per test
    4 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    Several years ago Tolya had n computer games and at some point of time he decided to burn them to CD. After that he wrote down the names of the games one after another in a circle on the CD in clockwise order. The names were distinct, the length of each name was equal to k. The names didn't overlap.

    Thus, there is a cyclic string of length n·k written on the CD.

    Several years have passed and now Tolya can't remember which games he burned to his CD. He knows that there were g popular games that days. All of the games he burned were among these g games, and no game was burned more than once.

    You have to restore any valid list of games Tolya could burn to the CD several years ago.

    Input

    The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 105) — the amount of games Tolya burned to the CD, and the length of each of the names.

    The second line of the input contains one string consisting of lowercase English letters — the string Tolya wrote on the CD, split in arbitrary place. The length of the string is n·k. It is guaranteed that the length is not greater than 106.

    The third line of the input contains one positive integer g (n ≤ g ≤ 105) — the amount of popular games that could be written on the CD. It is guaranteed that the total length of names of all popular games is not greater than 2·106.

    Each of the next g lines contains a single string — the name of some popular game. Each name consists of lowercase English letters and has length k. It is guaranteed that the names are distinct.

    Output

    If there is no answer, print "NO" (without quotes).

    Otherwise, print two lines. In the first line print "YES" (without quotes). In the second line, print n integers — the games which names were written on the CD. You should print games in the order they could have been written on the CD, it means, in clockwise order. You can print games starting from any position. Remember, that no game was burned to the CD more than once. If there are several possible answers, print any of them.

    Examples
    input
    3 1
    abc
    4
    b
    a
    c
    d
    output
    YES
    2 1 3
    input
    4 2
    aabbccdd
    4
    dd
    ab
    bc
    cd
    output
    NO
    分析:字典树上的匹配,因为最多有k个容器;
       把匹配好的编号分别放入相应的容器里,最后看是否满足n个即可;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <unordered_map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<ll,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    const int maxn=1e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,sz,cur_v;
    struct node
    {
        int nxt[26],id,num,pre,link;
    }trie[maxn*10*3];
    char a[maxn*11],b[maxn];
    set<int>p[maxn];
    void insert(int x)
    {
        int now=0;
        for(int i=0;i<k;i++)
        {
            int y=b[i]-'a';
            if(!trie[now].nxt[y]){
                trie[now].nxt[y]=++sz;
                trie[sz].pre=now;
                trie[sz].num=y;
            }
            now=trie[now].nxt[y];
        }
        trie[now].id=x;
    }
    int link(int v);
    int go(int v, int c) {
        if (trie[v].nxt[c]) {
            return trie[v].nxt[c];
        }
        if (v == 0) {
            return 0;
        }
        return trie[v].nxt[c] = go(link(v), c);
    }
    
    int link(int v) {
        if (trie[v].link) {
            return trie[v].link;
        }
        if (v == 0 || trie[v].pre == 0) {
            return 0;
        }
        return trie[v].link = go(link(trie[v].pre), trie[v].num);
    }
    void check(int x)
    {
        cur_v=go(cur_v,a[x]-'a');
        if(trie[cur_v].id)
        {
            p[(x-k+1)%k].insert(cur_v);
        }
    }
    int get_id(int x)
    {
        int now=0;
        for(int i=0;i<k;i++)
        {
            int y=a[i+x]-'a';
            now=trie[now].nxt[y];
        }
        return trie[now].id;
    }
    int main()
    {
        int i,j;
        scanf("%d%d%s",&n,&k,a);
        for(i=0;i<k;i++)a[i+n*k]=a[i];
        scanf("%d",&m);
        rep(i,1,m)
        {
            scanf("%s",b);
            insert(i);
        }
        for(i=0;i<n*k+k;i++)
        {
            check(i);
        }
        for(i=0;i<k;i++)
        {
            if(p[i].size()==n)
            {
                puts("YES");
                for(j=i;j<n*k+i;j+=k)printf("%d ",get_id(j));
                return 0;
            }
        }
        puts("NO");
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    Diffbot:开发者工具 将web内容转换成应用
    算法之道—形而之上谓之道
    css三个ppt
    SMB的NTLM认证过程与NTLM挑战的编程实现
    c++计算圆周率
    SVN总结
    struts2技术实现用户名唯一的验证处理详解
    数字常量
    二叉树的创建和遍历
    php变量的定义和作用域
  • 原文地址:https://www.cnblogs.com/dyzll/p/5967085.html
Copyright © 2011-2022 走看看