題意:
給定一個長度爲N≤104的加密句子,加密方式爲將句子每個單詞翻轉,並移除空格,句子字母全爲小寫
現給定M≤105個單詞wi,大小寫均有,|wi|≤103且∑wi≤106
現用這些單詞解密句子,每個單詞可以用多次,保證一種解密方式存在,多解輸出任意一種
分析:
一上來把單詞數看錯,看成1W個,寫了個kmp預處理轉移過程優化n3 dp到n2,終測T了
討論題目發現是10W個單詞,搞了個ac自動機上去,T更後面的case,−−,爆炸,果然記錄轉移是不行的
仔細想想這個dp,f[i]:=以i結尾的字符串是否可以解密,爲記錄答案直接存單詞長度就可以了
其實直接在trie上順推轉移就可以了,沒必要記錄轉移,只要1個答案本來就是n2的dp,被我強行搞成n3
時間復雜度爲O(n∗|wmax|)
其實判斷這個單詞在母串中是否出現過完全沒必要用trie啊,直接hash單詞,維護母串前綴哈希,然後順推轉移就可以了
//
// 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;
}
sb數錯0了寫的kmp代碼,就算不數錯也得T
10W個a的單詞,直接炸裂map記錄的預處理,明明可以繼續利用題目信息的,too young
//
// 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;
}