QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#346026 | #3199. Passwords | KhNURE_KIVI# | AC ✓ | 42ms | 3984kb | C++17 | 7.0kb | 2024-03-07 19:49:23 | 2024-03-07 19:49:24 |
Judging History
answer
//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <queue>
#include <bitset>
#include <numeric>
#include <array>
#include <cstring>
#include <random>
#include <chrono>
#include <cassert>
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<typename T>
bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef KIVI
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template<class ...Ts>
auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif
mt19937 rng(4242);
const int max_n = -1, inf = 1000111222;
class AhoCorasick {
public:
static const int min_c = 'a', max_c = 'z' + 1;
struct Node {
friend class AhoCorasick;
int parent, parent_c;
bool is_terminal;
Node(int parent, int parent_c): suffix_link(-1), parent(parent), parent_c(parent_c), closest_terminal(-1), is_terminal(false) {
memset(nxt, -1, sizeof(nxt));
};
private:
int nxt[max_c - min_c + 1], suffix_link;
int closest_terminal;
};
vector<Node> nodes;
AhoCorasick(): nodes({{0, 0}}) {}
AhoCorasick(int max_nodes) : nodes({{0, 0}}) {
nodes.reserve(max_nodes);
}
void clear() {
nodes.clear();
nodes.pb({0, 0});
}
int add(const string &s) {
int v = 0;
for (char cur_c : s) {
assert(min_c <= cur_c && cur_c <= max_c);
const int c = cur_c - min_c;
if(nodes[v].nxt[c] == -1) {
nodes[v].nxt[c] = nodes.size();
nodes.pb({v, c});
}
v = nodes[v].nxt[c];
}
nodes[v].is_terminal = true;
return v;
}
int get_nxt(int v, int c) {
assert(min_c <= c && c <= max_c);
c -= min_c;
return get_nxt_private(v, c);
}
int get_suffix_link(int v) {
if(nodes[v].suffix_link == -1) {
if(nodes[v].parent == 0) {
nodes[v].suffix_link = 0;
} else {
nodes[v].suffix_link = get_nxt_private(get_suffix_link(nodes[v].parent), nodes[v].parent_c);
}
}
return nodes[v].suffix_link;
}
int get_closest_terminal(int v) {
if(nodes[v].closest_terminal == -1) {
if(v == 0) {
nodes[v].closest_terminal = 0;
} else if(nodes[v].is_terminal) {
nodes[v].closest_terminal = v;
} else {
nodes[v].closest_terminal = get_closest_terminal(get_suffix_link(v));
}
}
return nodes[v].closest_terminal;
}
vector<vector<int> > get_suffix_link_tree() {
vector<vector<int> > g(nodes.size());
for(int i = 1; i < nodes.size(); i++) {
g[get_suffix_link(i)].pb(i);
}
return g;
}
vector<int> get_bfs_order() {
queue<int> q;
vector<int> res;
vector<char> used(nodes.size());
q.push(0);
used[0] = 1;
while(!q.empty()) {
int v = q.front();
q.pop();
res.pb(v);
for(int c = 0; c <= max_c - min_c; c++) {
if(!used[get_nxt_private(v, c)]) {
q.push(get_nxt_private(v, c));
used[get_nxt_private(v, c)] = 1;
}
}
}
return res;
}
private:
int get_nxt_private(int v, int c) {
if(nodes[v].nxt[c] == -1) {
if(v == 0) {
nodes[v].nxt[c] = 0;
} else {
nodes[v].nxt[c] = get_nxt_private(get_suffix_link(v), c);
}
}
return nodes[v].nxt[c];
}
};
const int mod = 1000003;
int add(int a, int b) {
return (a + b) % mod;
}
void solve() {
int a, b;
cin >> a >> b;
int n;
cin >> n;
vector<string> bad(n);
AhoCorasick aho = AhoCorasick(222);
for(auto& x : bad) {
cin >> x;
aho.add(x);
}
int amv = len(aho.nodes);
vector<bool> bad_v(amv);
for(int i = 0; i < amv; i++) {
int cur_v = i;
bool have_terminal = false;
while(cur_v != 0) {
have_terminal |= aho.nodes[cur_v].is_terminal;
cur_v = aho.get_suffix_link(cur_v);
}
bad_v[i] = have_terminal;
}
vector<pair<int, int> > transitions; //mask, char
for(int i = 'a'; i <= 'z'; i++) transitions.pb({(1 << 0), i}); //lowercase
for(int i = 'a'; i <= 'z'; i++) transitions.pb({(1 << 1), i}); //uppercase
//digits
{
transitions.pb({(1 << 2), 'o'}); //0
transitions.pb({(1 << 2), 'i'}); //1
transitions.pb({(1 << 2), 'z' + 1}); //2
transitions.pb({(1 << 2), 'e'}); //3
transitions.pb({(1 << 2), 'z' + 1}); //4
transitions.pb({(1 << 2), 's'}); //5
transitions.pb({(1 << 2), 'z' + 1}); //6
transitions.pb({(1 << 2), 't'}); //7
transitions.pb({(1 << 2), 'z' + 1}); //8
transitions.pb({(1 << 2), 'z' + 1}); //9
}
vector<vector<int> > dp(amv, vector<int>((1 << 3))), ndp(amv, vector<int>((1 << 3)));
dp[0][0] = 1;
int ans = 0;
for(int lenS = 1; lenS <= b; lenS++) {
for(int state = 0; state < amv; state++) {
for(int mask = 0; mask < 8; mask++) {
for(auto& x : transitions) {
int nstate = aho.get_nxt(state, x.se);
if(bad_v[nstate]) continue;
int nmask = (mask | (x.fi));
ndp[nstate][nmask] = add(ndp[nstate][nmask], dp[state][mask]);
}
}
}
for(int state = 0; state < amv; state++) {
for (int mask = 0; mask < 8; mask++) {
dp[state][mask] = ndp[state][mask];
ndp[state][mask] = 0;
}
}
if(lenS >= a) {
for(int v = 0; v < amv; v++) ans = add(ans, dp[v][7]);
}
}
cout << ans << '\n';
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
}
/*
KIVI
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3684kb
input:
3 5 9 swerc icpc fbi cia bio z hi no yes
output:
607886
result:
ok single line: '607886'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3908kb
input:
4 5 0
output:
887311
result:
ok single line: '887311'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
3 5 1 a
output:
223848
result:
ok single line: '223848'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
4 5 10 a c e g i k m o q s
output:
178702
result:
ok single line: '178702'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
3 4 26 a b c d e f g h i j k l m n o p q r s t u v w x y z
output:
0
result:
ok single line: '0'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3620kb
input:
3 5 8 fcup porto swerc ola hello no yes abc
output:
839906
result:
ok single line: '839906'
Test #7:
score: 0
Accepted
time: 1ms
memory: 3680kb
input:
3 5 10 attc tcag gtc gta aaaa ccaaa ttcg gacga aagcg cagd
output:
796623
result:
ok single line: '796623'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3876kb
input:
3 5 12 a baz cbaz dcbaz hendb endb ndb b nn onno ayesb yes
output:
511313
result:
ok single line: '511313'
Test #9:
score: 0
Accepted
time: 1ms
memory: 3916kb
input:
4 6 50 hph h pp hhphpp p ph hh hhpp hpphhp hhp ppphp pph pppp hphph phhphh hpph hphphp phphp phphh hhh pphp phhh pphh ppphh phphpp phh hpphhh hhhh pphph phpp phph hhhhph hpphh hp hpp pphpp hhhp phhp hhph ppph hppph phhhh ppppp hppp hhphph hphphh ppp hhhpph hpppp hppphp
output:
344974
result:
ok single line: '344974'
Test #10:
score: 0
Accepted
time: 2ms
memory: 3904kb
input:
3 6 50 wqog sjqr fcbqax y xpwwi j gklgnc bmhark qgaj uvfpwz hi nallwx obms oigkq uehl r fq uqe rbhbt luzt jxyu fgvrk s gidv jlagzj cyshm ixjxu vkhjh ibbw k zq b knav ayxeqm rncdp kzqdww cdkkw fjl hy l qsakh bgju yzx sf emuc lq wcaba wla yal e
output:
188830
result:
ok single line: '188830'
Test #11:
score: 0
Accepted
time: 2ms
memory: 3860kb
input:
3 10 25 axaxxx txrj tt tttaxjr xxxrtj xttarxxxtx xaxjr r jrrjrxjr aarajar txt x rt xatxt xttj xjja xra jxt ra taaxra aj rjr xxaxaaj xxttrjxa xxtajx
output:
687381
result:
ok single line: '687381'
Test #12:
score: 0
Accepted
time: 9ms
memory: 3904kb
input:
3 15 35 vxkqvdexdx zxvee mvozimqezvxqoe qev vxiovmmdxq xozzzm iqexzoxqievxze deezqqdkek zvzodz veizidqkemvziox zmzidxidezve koxdvdm ozzmii xvedxqmm dqziexoe qdmo dokieiodoqzdzix dke odqo imeemxvexxzov veeixmvdq vdveoqv ixmezo dqoeexizokemovo dddzeokm ozizmq izieei kexzkqmxoqkeo dkoez voqmoidimzvm qq...
output:
841929
result:
ok single line: '841929'
Test #13:
score: 0
Accepted
time: 9ms
memory: 3680kb
input:
10 15 45 aa rrraxrarrkakxaa kr rkrarxrkxrark kkkakarxrr rrkakarxrr axxxaxakkkxxkrk aaxrrkarxaxaxk xrxa aaaakxxkaraar rxrr axraaaaxxxxkxk xx rrxx xakkxrrx k rkarrxkkrx rara xxaarakax raraakkaaaxa kxxr kraxkaxaar x xkkkkxxkk rkkarr krxkkxkrxkxkxax araxkkrx arar aaa xaarkxarkxxk xrk krrkkxrxa xaxrkxrax...
output:
322551
result:
ok single line: '322551'
Test #14:
score: 0
Accepted
time: 19ms
memory: 3788kb
input:
18 18 50 pnn pnppplnlpnnllpplnn pllnpppl llpppplnppnnppnlp lln lplnnnlnplnn nlllppllplppplll lllppp plplplpllpppnllll ppplpnplnnpp ppnpnnn nnnnpnnppnnnnpn npllnppnnl pnpnl pnpll npll lnlnlpppplpnplpnp npnlnnlnllpplpp nlplpp nnppnllppllpnnll ppnlnlnplnllnl pnplplplllpnnp lpnnppnlppnnn ppnlpnp nplnpnp...
output:
389309
result:
ok single line: '389309'
Test #15:
score: 0
Accepted
time: 16ms
memory: 3980kb
input:
4 19 50 psmx wwpswpqqxymyytyx xp ywmasrmddtppwawdy yzsjgzjxazggprsnpa nngnqdgqpmpzjwt dznrtstpmrmyzzra qryzdgjpdxtpxypatzs zmyn wasyxsppqjpxzaradzw xmyngqtrgnpdnmywy spypzgmxq dxwrtnpg pqp jpqsxrqgxjrq dpwqs agzayxgqzwjwztr xpqtzxry rx njrr ajgwdwr ymjysjzgnqpwdxzjj pqazyxxryrmtzwmxn ms tqqrztnrrtzn...
output:
684686
result:
ok single line: '684686'
Test #16:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
3 20 0
output:
998674
result:
ok single line: '998674'
Test #17:
score: 0
Accepted
time: 30ms
memory: 3780kb
input:
3 20 50 gwogsnogslv dlwngqvnvvjod qvvwssloqlvjwld lgndnlwosjsdg qqjqsdolwddjs dnoojjwwnlodd sdsojlnvgwjqoslnno jddnnjwqvnjq solvgologqndgosjdgn vwdolvqsndnowoqwd qlwlogwqwndqlqsgjvvw onovnlwdowvwwn sddsvgonqnjlgd svsdlngdsndlqnqddjg dgvsngdsgq ggvdloloqsssnogv lsngqgodgwjosodq wsqvoolnqsjd joswdggll...
output:
507317
result:
ok single line: '507317'
Test #18:
score: 0
Accepted
time: 42ms
memory: 3984kb
input:
3 20 50 ploynntcvajxahnainmo vhgcnjxidsfbudfxozng vjsljvycjydraaxmfwec nkpkhtwpjqdserapecfw tufqdnmuodfrxlfafcia aerajvorujijmgebsvyt layevdwrdirqdbjiycgf suwgmhmnvqyhsilqyaok orundlohsmzncfbjifwg xallxiszxkxuahclwdjc uqnioqwsglsgdjjltvsn nkcgdhkhjeaocrncoyxu jcylizcagdlqfqyknmuc yqojefflbxbszhftdnj...
output:
17312
result:
ok single line: '17312'