QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#314095 | #1193. Ambiguous Encoding | yinhee | WA | 1ms | 4120kb | C++14 | 2.1kb | 2024-01-25 12:41:39 | 2024-01-25 12:41:39 |
Judging History
answer
// Problem: P6681 [CCO2019] Bad Codes
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P6681
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// Written by yinhee.
#include<bits/stdc++.h>
using namespace std;
namespace my_std{
#define mems(x,y) memset(x,y,sizeof x)
#define Mp make_pair
#define eb emplace_back
#define gc getchar
#define pc putchar
#define fi first
#define se second
#define il inline
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define drep(i,a,b) for(int i=(a);i>=(b);i--)
#define go(i,u) for(int i=head[u];i;i=e[i].nxt)
typedef long long ll;
typedef pair<int,int> pii;
template<typename T>inline T read(){
T x=0,f=1;char c=gc();
while(!isdigit(c)){if(c=='-')f=-1;c=gc();}
while(isdigit(c))x=x*10+c-48,c=gc();
return x*f;
}
}
using namespace my_std;
const int N=1e4+7,M=1e6+7,inf=0x3f3f3f3f,mod=0;
#define ID(i,j) ((i-1)*(m+1)+j+1)
int n,m,dis[N];
string s[N];bool vis[N];
priority_queue<pii> q;
int tot,head[N];
struct node{int to,nxt,cw;}e[M];
il void add(int u,int v,int w){e[++tot]={v,head[u],w},head[u]=tot;}
void Yorushika(){
scanf("%d%d",&n,&m);
rep(i,1,n)cin>>s[i];
rep(i,1,n){
int l=s[i].size();
rep(j,0,l-1){
rep(k,1,n){
int len=l-j;
if(k==i&&!j)continue;
if(s[k].size()>=len){
if(s[i].substr(j,len)==s[k].substr(0,len)){
int w=s[k].size()-len;
add(ID(i,len),ID(k,w),w);
}
}else{
// printf("%d %d\n",i,j);
int sz=s[k].size();
if(s[i].substr(j,sz)==s[k].substr(0,sz)){
int w=len-sz;
add(ID(i,len),ID(i,w),0);
}
}
}
}
}
mems(dis,0x3f);
rep(i,1,n){
int u=ID(i,s[i].size());
dis[u]=s[i].size(),q.push(Mp(-dis[u],u));
}
while(q.size()){
int u=q.top().se;q.pop();
if(vis[u])continue;
vis[u]=1;
go(i,u){
int v=e[i].to;
if(dis[v]<=dis[u]+e[i].cw)continue;
dis[v]=dis[u]+e[i].cw,q.push(Mp(-dis[v],v));
}
}
int ans=inf;
rep(i,1,n)ans=min(ans,dis[ID(i,0)]);
printf("%d\n",ans<1e9?ans:-1);
}
signed main(){
int t=1;
// scanf("%d",&t);
while(t--)
Yorushika();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 4120kb
input:
3 0 01 10
output:
0
result:
wrong answer expected '3', found '0'