QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#283160#1193. Ambiguous EncodingNYCU_templateWA 1ms4668kbC++202.8kb2023-12-13 23:17:062023-12-13 23:17:07

Judging History

你现在查看的是最新测评结果

  • [2023-12-13 23:17:07]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4668kb
  • [2023-12-13 23:17:06]
  • 提交

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;
        for(auto j : exact[now]) res.push_back(j);
        now = trie[now][d];
    }

    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";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4448kb

input:

3
0
01
10

output:

3

result:

ok answer is '3'

Test #2:

score: 0
Accepted
time: 1ms
memory: 4668kb

input:

3
00
01
1

output:

0

result:

ok answer is '0'

Test #3:

score: 0
Accepted
time: 1ms
memory: 4380kb

input:

3
00
10
1

output:

0

result:

ok answer is '0'

Test #4:

score: 0
Accepted
time: 1ms
memory: 4356kb

input:

10
1001
1011
01000
00011
01011
1010
00100
10011
11110
0110

output:

13

result:

ok answer is '13'

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 4616kb

input:

3
1101
1
10

output:

0

result:

wrong answer expected '4', found '0'