【暑训排位 #5 H】 Spy Syndrome 2 字典树 暴力优化

78 篇文章 0 订阅
4 篇文章 0 订阅

After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can’t use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique.

For a given sentence, the cipher is processed as:

Convert all letters of the sentence to lowercase.
Reverse each of the words of the sentence individually.
Remove all the spaces in the sentence.
For example, when this cipher is applied to the sentence

Kira is childish and he hates losing

the resulting string is

ariksihsidlihcdnaehsetahgnisol

Now Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10 000) — the length of the ciphered text. The second line consists of n lowercase English letters — the ciphered text t.

The third line contains a single integer m (1 ≤ m ≤ 100 000) — the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word w i (|w i| ≤ 1 000) consisting of uppercase and lowercase English letters only. It’s guaranteed that the total length of all words doesn’t exceed 1 000 000.

Output
Print one line — the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.

Examples
Input
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
Output
Kira is childish and he hates losing
Input
12
iherehtolleh
5
HI
Ho
there
HeLLo
hello
Output
HI there HeLLo
Note
In sample case 2 there may be multiple accepted outputs, “HI there HeLLo” and “HI there hello” you may output any of them.

题意:用一个去掉空格、小写化、翻转过的字符串在m个字符串中找到匹配的输出原句

思路:

1.对于翻转:字典树存的时候也反着存就好了。
2.对于小写:存的时候用转化一下,同时记录拿一个下标对应哪一个字符串。叶结点的color[i]记录的即是当前字符串的序号。
3.然后就正着匹配,找到一个当前i是一个字符串结尾的时候,就到下一个位置继续匹配,知道遍历完整个字符串。
4.注意到不能说每找到一个匹配的就输出一个,因为当前这个匹配到的字符串可能是后面才用上的。所以就要对每个可能的位置dfs一下看看能不能走完。
5.注意一点优化,不要用string,否则TL飞。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include <stack>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define maxn 100000+500
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int maxm = 100000+5;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline int read(){ int x = 0;char ch = getchar();while(ch>'9'||ch<'0') ch = getchar();while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x; }

const int MAX_NODE = 1e6+50;
const int CHARSET = 50;
int trie[MAX_NODE][CHARSET] = {0};
int color[MAX_NODE] = {0};
int k = 1;

char ans[MAX_NODE][1005];
int res[MAX_NODE];
int cur = 0;
int Len[MAX_NODE];
int n ;
void insert(string w, int idx)
{
    int len = w.size();
    int p = 0;
    for(int i=len-1; i>=0; i--)
    {
        if(w[i]>='A'&&w[i]<='Z') w[i] += 32;
        int c = w[i] - 'a';
        if(!trie[p][c])         //看看第p位置往下有没有以c开头的字符串
        {
          //  cout<<p<<' '<<c<<' '<<w[i]<<endl;
            trie[p][c] = k;     //没有的话就新增标记
            k++;        //更新
        }
        p = trie[p][c];     //p指针往下移动
    }
    color[p] = idx;
}

bool flag = false;
void search(string s, int pos)
{
    //cout<<pos<<endl;
    if(flag) return;
    if(pos>=n)
    {
        //cout<<"wa"<<endl;
        for(int i=0; i < cur; i++)
        cout<<ans[res[i]]<<' ';
        cout<<'\n';
        flag = true;
        return;
    }
    int len = s.size();
    int p = 0;
    for(int i=pos; i<len; i++)
    {
       // cout<<s[i]<<endl;
        int c = s[i] - 'a';
        if(!trie[p][c]) return ;       //如果没遍历完就到头了,说明不存在
        p = trie[p][c];  //否则往下走
        if(color[p])
        {
            res[cur++] = color[p];
            search(s,pos+Len[color[p]]);
            cur --;
        }
    }

}

int main()
{
        scanf("%d",&n);
        flag = false;
        cur = 0;
        k = 1;
        string s;
        cin>>s;
        int m; m = read();
        rep(i,1,m)
        {
            scanf("%s",ans[i]);
            string ss(ans[i]);
            insert(ss,i);
            Len[i] = ss.size();
        }
        search(s,0);
        return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值