QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#283161 | #1193. Ambiguous Encoding | NYCU_template | WA | 1ms | 4320kb | C++20 | 2.8kb | 2023-12-13 23:19:08 | 2023-12-13 23:19:08 |
Judging History
answer
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include <bits/stdc++.h>
using namespace std;
const int N = 1005, W = 2, L = 16;
string s[N];
int n, last_pos = 0, trie[N * L][W];
vector<int> id[N * L], exact[N * L];
void Insert(string &ss, int i) {
int now = 0;
for(auto c : ss) {
int d = (c - '0');
if(trie[now][d] == 0)
trie[now][d] = ++last_pos;
now = trie[now][d];
id[now].push_back(i);
}
exact[now].push_back(i);
}
vector<int> query_longer(string &ss) {
int now = 0;
for(auto c : ss) {
int d = (c - '0');
if(trie[now][d] == 0) return {};
now = trie[now][d];
}
return id[now];
}
vector<int> query(string &ss) {
int now = 0;
vector<int> res;
for(auto c : ss) {
int d = (c - '0');
if(trie[now][d] == 0) return res;
now = trie[now][d];
for(auto j : exact[now]) res.push_back(j);
}
for(auto j : id[now]) res.push_back(j);
return res;
}
map<string, bool> vis;
map<string, int> dis;
void DFS(string cur) {
if(vis[cur]) return;
vis[cur] = true;
dis[cur] = -1;
vector<int> js = query(cur);
int mi_len = -1;
for(auto j : js) {
if(s[j] == cur) {
dis[cur] = cur.size();
return;
}
}
for(auto j : js) {
if(s[j].size() < cur.size()) {
string nxt = cur.substr(s[j].size());
DFS(nxt);
if(dis[nxt] != -1) {
if(mi_len == -1 || mi_len > dis[nxt] + (int)s[j].size()) mi_len = dis[nxt] + s[j].size();
}
} else {
string nxt = s[j].substr(cur.size());
DFS(nxt);
if(dis[nxt] != -1) {
if(mi_len == -1 || mi_len > dis[nxt] + (int)cur.size()) mi_len = dis[nxt] + cur.size();
}
}
}
if(mi_len > 0) dis[cur] = mi_len;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> n;
for(int i = 0; i < n; i++) {
cin >> s[i];
}
// sort(s, s + n, [](string &a, string &b) {
// return a.size() < b.size();
// });
for(int i = 0; i < n; i++) {
Insert(s[i], i);
}
int ans = 1e9;
for(int i = 0; i < n; i++) {
vector<int> js = query_longer(s[i]);
for(int j : js) {
if(i != j) {
string start = s[j].substr(s[i].size());
DFS(start);
if(dis[start] != -1) {
ans = min(dis[start] + (int)s[i].size(), ans);
}
}
}
}
if(ans == 1e9) ans = 0;
cout << ans << "\n";
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 4236kb
input:
3 0 01 10
output:
3
result:
ok answer is '3'
Test #2:
score: 0
Accepted
time: 1ms
memory: 4236kb
input:
3 00 01 1
output:
0
result:
ok answer is '0'
Test #3:
score: 0
Accepted
time: 1ms
memory: 4240kb
input:
3 00 10 1
output:
0
result:
ok answer is '0'
Test #4:
score: 0
Accepted
time: 0ms
memory: 4300kb
input:
10 1001 1011 01000 00011 01011 1010 00100 10011 11110 0110
output:
13
result:
ok answer is '13'
Test #5:
score: 0
Accepted
time: 1ms
memory: 4232kb
input:
3 1101 1 10
output:
4
result:
ok answer is '4'
Test #6:
score: 0
Accepted
time: 1ms
memory: 4316kb
input:
100 1010011110001 00000100010101 011010100100001 11100001100011 10010001010 1110001110110011 01001100111 1010100110010100 00000100111010 100111001100101 0010000000010 0111110011100110 0100111000001 1100101111110001 1100100110001 10100011010 10100000011000 1111001111001110 000000000101011 10101111011...
output:
102
result:
ok answer is '102'
Test #7:
score: 0
Accepted
time: 1ms
memory: 4244kb
input:
10 0110 001100 0100 0001 100 001010 0010 100010 1100 11101
output:
10
result:
ok answer is '10'
Test #8:
score: 0
Accepted
time: 1ms
memory: 4320kb
input:
10 1001 10111 11011 00010 001 001100 110 01110 111101 11100
output:
10
result:
ok answer is '10'
Test #9:
score: -100
Wrong Answer
time: 1ms
memory: 4232kb
input:
10 0001 0101 1001 101 0010 010 00000 1000 00001 111101
output:
11
result:
wrong answer expected '10', found '11'