QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#350321 | #1193. Ambiguous Encoding | zjy0001 | WA | 0ms | 4356kb | C++14 | 1.4kb | 2024-03-10 17:14:59 | 2024-03-10 17:15:00 |
Judging History
answer
#include<bits/stdc++.h>
#define LL long long
#define LLL __int128
#define uint unsigned
#define ldb long double
#define uLL unsigned long long
using namespace std;
const int N=1e3+5,M=2e4+5,INF=1e9+7;
int n,cnt,D[M];
string s[N];
vector<int>id[N];
map<string,bool>mp;
vector<pair<int,int>>G[M];
priority_queue<pair<int,int>>Q;
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n;
for(int i=1;i<=n;++i){
cin>>s[i],mp[s[i]]=1,id[i].resize(s[i].length());
for(int j=0;j<s[i].length();++j)id[i][j]=cnt++;
}
for(int i=1;i<=n;++i)for(int j=1;j<s[i].length();++j)
for(int k=1;k<=n;++k){
int l=min(j,(int)s[k].length());
if(s[k].substr(0,l)==s[i].substr(s[i].length()-j,l))
if(l==s[k].length())G[id[i][j]].emplace_back(id[i][j-l],l);
else G[id[i][j]].emplace_back(id[k][s[k].length()-j],l);
}
fill(D,D+cnt,INF);
for(int i=1;i<=n;++i)
for(int j=1;j<s[i].length();++j)
if(mp.count(s[i].substr(0,j)))
D[id[i][s[i].length()-j]]=j,
Q.emplace(-j,id[i][s[i].length()-j]);
while(!Q.empty()){
const auto [d,u]=Q.top();Q.pop();
if(-d!=D[u])continue;
for(auto [v,w]:G[u])if(D[v]>D[u]+w)D[v]=D[u]+w,Q.emplace(-D[v],v);
}
int ans=INF;
for(int i=1;i<=n;++i)ans=min(ans,D[id[i][0]]);
cout<<(ans>=INF?-1:ans)<<'\n';
return 0;
}
/*
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 4284kb
input:
3 0 01 10
output:
3
result:
ok answer is '3'
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 4356kb
input:
3 00 01 1
output:
-1
result:
wrong answer expected '0', found '-1'