QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#72958 | #2902. Shortest Missing Subsequences | codingsword# | AC ✓ | 275ms | 140876kb | C++17 | 2.7kb | 2023-01-21 04:12:11 | 2023-01-21 04:12:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define nl '\n'
const int DEBUG = false, ALPHA = 26;
vector<vi> nxt;
vi valid;
int shortestLength;
void init(string &str, string &alphabet)
{
// set up alphabet
valid = vi (ALPHA, false);
for(int i = 0; i< alphabet.size(); i++)
valid[alphabet[i] - 'a'] = true;
if(DEBUG) { cout << "alphabet:"; for(int i = 0; i < ALPHA; i++) cout << valid[i]; cout << nl;}
// set up next index
nxt = vector<vi> (str.size() + 1, vi (26));
vi alpha (ALPHA, str.size());
for(int i = str.size() - 1; i >= 0; i--)
{
for(int j = 0; j < ALPHA;j ++)
{
nxt[i+1][j] = alpha[j];
}
alpha[str[i] - 'a'] = i;
}
nxt[0] = alpha;
if(DEBUG)
{
cout << "NEXT: " << nl;
for(int i = 0; i < nxt.size(); i++)
{
for(int j = 0; j < nxt[i].size();j ++)
cout << nxt[i][j] << " ";
cout << nl;
}
cout << endl;
}
}
int getLength()
{
int len = 0;
deque<int> q;
deque<int> q2;
vi visited (nxt.size());
visited[0] = true;
q.push_back(0);
while(true)
{
len ++;
if(DEBUG) cout << "Current Shortest Len = " << len << nl;
while(!q.empty())
{
int idx = q.front();
q.pop_front();
if(DEBUG) cout << "\tchecking idx " << idx << nl;
for(int alpha = 0; alpha < ALPHA; alpha++)
{
if(!valid[alpha]) continue;
int jump = nxt[idx][alpha] + 1;
if(jump >= nxt.size())
return len;
if(visited[jump]) continue;
visited[jump] = true;
q2.push_back(jump);
if(DEBUG) cout << "\t\tadding idx " << jump << " to deque " << nl;
}
}
q = q2;
q2 = deque<int> (0);
}
}
int query(string &q)
{
if(q.size() != shortestLength) return false;
int s = 0;
for(int i = 0; i < q.size(); i++)
{
if(!valid[q[i] - 'a']) return false; // invalid alphabet char
s = nxt[s][q[i] - 'a'] + 1;
if(s >= nxt.size()) return true;
}
return false;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int q;
string alphabet, str, queryStr;
cin >> alphabet;
cin >> str;
init(str, alphabet);
shortestLength = getLength();
if(DEBUG) cout << "shortest length = " << shortestLength << nl;
cin >> q;
while(q--)
{
cin >> queryStr;
cout << query(queryStr) << nl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3492kb
input:
abcd abcccabac 3 cbb cbba cba
output:
0 0 0
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 3512kb
input:
z zzzzzzz 10 z zz zzz zzzz zzzzz zzzzzz zzzzzzz zzzzzzzz zzzzzzzzz zzzzzzzzzz
output:
0 0 0 0 0 0 0 1 0 0
result:
ok 10 lines
Test #3:
score: 0
Accepted
time: 275ms
memory: 140876kb
input:
abcdefghijklmnopqrstuvwxyz oaxsgfhkwuecvdrltjzpqibnymtaympfvuzogrqkwhbdcsjleixnojmletqrbkdzfhawpsxcvyniughnqibtzjpewovluyrdfmakxscgcyhmqbieontgwufalzjsprvkdxzfqsrijldncaphwxyutbgevmokltvqhkprunydecwmgxoijzbfsacafhozxyljvnrmbdupeqiswgtkbhwxptqayglfovirnjuemkdczsmyadiqnwtxjbhlsvokpucgefrzvicbdnkrjmxgf...
output:
0 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
result:
ok 26 lines
Test #4:
score: 0
Accepted
time: 155ms
memory: 140864kb
input:
d dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd...
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 49ms
memory: 140772kb
input:
abcdefghijklmnopqrstuvwxyz wktxlbgnmkyuvhmjpxxfndhdtcqugxnsvpxdictbxwrimocjliqqxfomlwngbvyoyacswksvurqaqmvwigtiswbfyofgvdvolcciwnymchohsdoivyofupophlcpukipgheeveaqrgmbcrtdwdybwcxsyvorkvsnhnvgwfxrpvykphlmrqsyujciqdmpqiwegathdsfjueilvbcudwsfbvpqxyoprbadqepvpdkcjvdapafesdmftubyraolqcvgbfbqhjlebnmbpcskf...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 ...
result:
ok 10000 lines
Test #6:
score: 0
Accepted
time: 78ms
memory: 140836kb
input:
abcdefghijklmnopqrstuvwxyz wktxlbgnmkyuvhmjpxxfndhdtcqugxnsvpxdictbxwrimocjliqqxfomlwngbvyoyacswksvurqaqmvwigtiswbfyofgvdvolcciwnymchohsdoivyofupophlcpukipgheeveaqrgmbcrtdwdybwcxsyvorkvsnhnvgwfxrpvykphlmrqsyujciqdmpqiwegathdsfjueilvbcudwsfbvpqxyoprbadqepvpdkcjvdapafesdmftubyraolqcvgbfbqhjlebnmbpcskf...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 ...
result:
ok 10000 lines
Test #7:
score: 0
Accepted
time: 176ms
memory: 140856kb
input:
abcdefghijklmnopqrstuvwxyz ngmgaeihgmnehfjdicecmbjmeilmjceblbnkfhibfgfjkdihhnieamiablnglnmkkajhnnfdlflnbdjddmcmihbbfihbeielbifnidmjijehbjmgfjdecdncajkehbbkmccanblnikgnlieimdndkjngjehhkklmfbfjbhjkfnddalebldfmcfgnabmcnldanjkijkbabkdjnjbgbfnbajadclbhdlmakaigjbnebdbkefgcaihajblgdeflhnjclkdmamkcncfiebjhk...
output:
1 0 1 1 1 0 1 0 0 1 0 1 0 1 0 0 0 0 1 1 1 0 0 0 1 1 0 0 1 1 0 0 0 1 0 0 0 1 1 0 0 0 0 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 1 1 1 1 0 0 0 1 0 0 0 1 1 0 1 0 1 0 0 1 1 1 1 1 0 0 1 0 0 1 0 1 0 0 1 1 0 1 0 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 1 0 0 0 1 0 1 1 0 1 0 0 0 0 1 1 1 1 0 0 0 1 0 0 1 0 1 0 ...
result:
ok 1000000 lines
Test #8:
score: 0
Accepted
time: 2ms
memory: 3408kb
input:
abcd aabcdacbacdabcabdcccbddadbcdbacbdad 8 dddaadda dddaaddb dddaaddc dddaaddd dddaadaa dddaadab dddaadac dddaadad
output:
0 0 0 0 0 0 0 0
result:
ok 8 lines
Test #9:
score: 0
Accepted
time: 0ms
memory: 3376kb
input:
f f 2 f ff
output:
0 1
result:
ok 2 lines
Test #10:
score: 0
Accepted
time: 1ms
memory: 3492kb
input:
xy x 6 x y xx xy yx yy
output:
0 1 0 0 0 0
result:
ok 6 lines
Test #11:
score: 0
Accepted
time: 95ms
memory: 140736kb
input:
abcdefghijklmnopqrstuvwxyz irxwwhliqvpptnwkhqgyxopsvwguplvwksziymgggtrgzynukovgrkwpzqrknkukvpzmzgoxzrztijtvymivmyvuqzoqxzujmlsyqxzqosxgqlvwootjnryuwuvnhptqiwjqxonqvgpgmuxsssotjjqkgwkrzyrkoqnzxsnpzltrsvgowxxyzvqwiqwrkozhqynrrkzqoqxnongrrtwsmpsgwktjrsjyhwrwxxzlgxzuhhygpgvvllliwtjnmhjjlrzjtltumpumqpigm...
output:
0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 ...
result:
ok 200000 lines
Test #12:
score: 0
Accepted
time: 2ms
memory: 3448kb
input:
abcdefghijklmnopqrstu orslbpmouunpnnfcgtbplsqimmroonbfbgoehmkohbdekngpkkudgpeknramojnffimsborgoheggfshaqrhteepcmchqdtsu 1 au
output:
0
result:
ok single line: '0'
Test #13:
score: 0
Accepted
time: 2ms
memory: 3416kb
input:
ab aab 1 bb
output:
1
result:
ok single line: '1'
Test #14:
score: 0
Accepted
time: 2ms
memory: 3420kb
input:
ab ba 1 bb
output:
1
result:
ok single line: '1'
Test #15:
score: 0
Accepted
time: 2ms
memory: 3432kb
input:
abc baaaaac 1 cc
output:
1
result:
ok single line: '1'
Test #16:
score: 0
Accepted
time: 2ms
memory: 3404kb
input:
abc abcccabac 3 cbb cbba cba
output:
1 0 0
result:
ok 3 lines