QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#314536 | #1193. Ambiguous Encoding | OFforest_1273 | RE | 0ms | 0kb | C++14 | 3.2kb | 2024-01-25 19:54:51 | 2024-01-25 19:54:51 |
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef genshin
#include <experimental/iterator>
#endif
const int INF = 0x3F3F3F3F;
const int maxn = 16;
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> Q;
int dis[maxn + 1][1 << maxn];
signed main() { ios::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
vector<pair<int, int>> num(n); // len, mask
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
int x = 0, g = 0;
for (char c : s) {
x |= (c & 1) << g;
g++;
}
num[i] = {s.size(), x};
}
memset(dis, 0x3f, sizeof dis);
for(int i=1;i<=n;++i)/*初始化*/
for(int j=1;j<=n;++j){
int len1=num[i].first,len2=num[j].first,val1=num[i].second,val2=num[j].second;
if((len1==len2&&val1==val2)||len1<len2)continue;
if((val1&((1<<len2)-1))!=val2)continue;
int nxt_len=len1-len2,nxt_val=val1>>len2;
if(dis[nxt_len][nxt_val]>len1)dis[nxt_len][nxt_val]=len1,Q.push({len1,nxt_len,nxt_val});
}
while(!Q.empty()){
auto [d,len,val]=Q.top();Q.pop();
if(d>dis[len][val])continue;
for(int i=1;i<=n;++i){
int len2=num[i].first,val2=num[i].second,nxt_len,nxt_val,flag=0;
if(len>=len2&&((val&((1<<len2)-1))==val2))nxt_len=len-len2,nxt_val=val>>len2,flag=1;
else if(len<len2&&(val2&((1<<len)-1))==val)nxt_len=len2-len,nxt_val=val2>>len,flag=1;
if(!flag)continue;
int cost=max(0,len2-len);
if(d+cost<dis[nxt_len][nxt_val])dis[nxt_len][nxt_val]=d+cost,Q.push({d+cost,nxt_len,nxt_val});
}
}
// for (const auto &[len1, mask1] : num)
// for (const auto &[len2, mask2] : num) {
// if (len1 == len2 && mask1 == mask2)
// continue;
// if (len1 < len2)
// continue;
// if ((mask1 & ((1 << len2) - 1)) != mask2)
// continue;
// int nxt_mask = mask1 >> len2;
// int nxt_len = len1 - len2;
// if (dis[nxt_len][nxt_mask] > len1) {
// dis[nxt_len][nxt_mask] = len1;
// pq.push({len1, nxt_len, nxt_mask});
// }
// }
// while (!pq.empty()) {
// auto [d, len, mask] = pq.top();
// pq.pop();
// if (d > dis[len][mask])continue;
// for (const auto &[new_len, new_mask] : num) {
// int nxt_mask, nxt_len;
// if (new_len <= len) {
// if ((mask & ((1 << new_len) - 1)) != new_mask)
// continue;
// nxt_mask = mask >> new_len;
// nxt_len = len - new_len;
// }
// else {
// if ((new_mask & ((1 << len) - 1)) != mask)
// continue;
// nxt_mask = new_mask >> len;
// nxt_len = new_len - len;
// }
// int cost = max(0, new_len - len);
// if (dis[nxt_len][nxt_mask] > d + cost) {
// dis[nxt_len][nxt_mask] = d + cost;
// pq.push({d + cost, nxt_len, nxt_mask});
// }
// }
// }
if (dis[0][0] == INF)
cout << "0\n";
else
cout << dis[0][0] << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
3 0 01 10