Manthan, Codefest 16 C. Spy Syndrome 2(dp、trie|哈希)

題意:

N104,,,
M105wi,,|wi|103wi106
,,,

分析:

,1W,kmpn3 dpn2,T
10W,ac,Tcase,,,
dp,f[i]:=i,
trie,,1n2dp,n3
O(n|wmax|)


trie,hash,,

trie 代碼:

//
//  Created by TaoSama on 2016-02-27
//  Copyright (c) 2016 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int M = 1e6 + 10, S = 26;

int n, m;
string s, t[N];
int f[N];

struct AcAutomata {
    int root, sz;
    int nxt[M][S], id[M];
    int newNode() {
        id[sz] = -1;
        memset(nxt[sz], -1, sizeof nxt[sz]);
        return sz++;
    }
    void init() {
        sz = 0;
        root = newNode();
    }
    void insert(string & s, int x) {
        int u = root;
        for(int i = s.size() - 1; ~i; --i) {
            int c = tolower(s[i]) - 'a';
            int &v = nxt[u][c];
            if(v == -1) v = newNode();
            u = v;
        }
        id[u] = x;
    }

    void query(int idx) {
        int u = root;
        for(int i = idx; i < s.size(); ++i) {
            u = nxt[u][s[i] - 'a'];
            if(u == -1) return;
            if(~id[u] && !f[i + 1])
                f[i + 1] = id[u];
        }
    }
} ac;


int main() {
#ifdef LOCAL
    freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
//  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    cin >> n >> s >> m;
    ac.init();
    for(int i = 1; i <= m; ++i) {
        cin >> t[i];
        ac.insert(t[i], i);
    }
    f[0] = 1;
    for(int i = 0; i < n; ++i) {
        if(!f[i]) continue;
        ac.query(i);
    }
    vector<int> ans;
    int u = n;
    while(u) {
        ans.push_back(f[u]);
        u -= t[f[u]].size();
    }
    for(int i = ans.size() - 1; ~i; --i)
        cout << t[ans[i]] << (" \n"[!i]);
    return 0;
}

sb0kmp,T
10Wa,map,,too young

TLE on test 67 代碼:

//
//  Created by TaoSama on 2016-02-27
//  Copyright (c) 2016 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, m, nxt[N];
string s, t[N];
map<pair<int, int>, int> mp;

void getNxt(string& t) {
    nxt[0] = -1;
    int i = 0, j = -1;
    while(i < t.size()) {
        if(j == -1 || t[i] == t[j]) nxt[++i] = ++j;
        else j = nxt[j];
    }
}

void kmp(string t, int id) {
    reverse(t.begin(), t.end());
    for(int i = 0; i < t.size(); ++i) t[i] = tolower(t[i]);
    int i = 0, j = 0, sz = t.size();
    getNxt(t);
    while(i < s.size()) {
        if(j == -1 || s[i] == t[j]) ++i, ++j;
        else j = nxt[j];
        if(j == sz) mp[ {i - sz + 1, i}] = id;
    }
}

int f[N];
pair<int, int> pre[N];

int main() {
#ifdef LOCAL
    freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
//  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    cin >> n >> s >> m;
    for(int i = 1; i <= m; ++i) {
        cin >> t[i];
        kmp(t[i], i);
    }
    f[0] = 1;
    for(int i = 1; i <= n; ++i) {
        for(int j = 0; j < i; ++j) {
            if(!f[j]) continue;
            if(mp.count({j + 1, i})) {
                f[i] = 1;
                pre[i] = {j, mp[{j + 1, i}]};
                break;
            }
        }
    }
    vector<int> ans;
    int u = n;
    while(u) {
        ans.push_back(pre[u].second);
        u = pre[u].first;
    }
    for(int i = ans.size() - 1; ~i; --i)
        cout << t[ans[i]] << (" \n"[!i]);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章