跳到正文

题解

题解:P2506 [SCOI2008] 劣质编码

Im_a_dog约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年2月28日
收录
2026年8月5日
更新
2026年11月14日
篇幅
约 2 分钟
获赞
1
题目
P2506

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#define LL long long
#define vi vector<int>
using namespace std;
int n,id[35][55],totl,dfn;
string str[35];
vi s,t;map<vi,int>mp;
queue<vi>q;
int main(){
	scanf("%d",&n);
	for(int i=1,l;i<=n;i++){
		cin>>str[i];l=str[i].size();totl+=l;
		if(!l){puts("0");return 0;}
		for(int j=0;j<l;j++)id[i][j]=dfn++;
	}
	s.resize(totl);t.resize(totl);
	for(int i=1;i<=n;i++)s[id[i][0]]=1;
	mp[s]=0;q.push(s);
	while(!q.empty()){
		vi u=q.front();q.pop();int ns=mp[u];
		for(char ch='0';ch<='1';ch++){
			for(int i=0;i<totl;i++)t[i]=0;
			int ed=0;
			for(int i=1,l;i<=n;i++){
				l=str[i].size();
				for(int j=0;j<l;j++)if(str[i][j]==ch){
					if(j==l-1)ed+=u[id[i][j]];else t[id[i][j+1]]=u[id[i][j]];
				}
			}
			if(ed>=3){printf("%d\n",ns+1);return 0;}
			for(int i=1;i<=n;i++)t[id[i][0]]=ed;
			if(!mp.count(t))mp[t]=ns+1,q.push(t);
		}
	}
	puts("-1");
	return 0;
}
原文
存档评论1 条抓取于