首页 > 其他分享 >【题解】P4683 [IOI2008] Type Printer

【题解】P4683 [IOI2008] Type Printer

时间:2022-10-31 11:58:24浏览次数:75  
标签:IOI2008 Printer int 题解 5000007 include Type

题目传送门:P4683 [IOI2008] Type Printer
板子题
贪心+字典树+dfs
贪心:把最长的字符留在最后打或者最先打
把每个字母插入字典树
对trie树dfs一遍

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int n,ch[5000007][28],val[5000007],vis[5000007],f,maxx=-1,cnt,idx;
char s[30000][22],ans[5000007];

inline void insert(char s[])
{
	int p=0;
	for(int i=0;i<strlen(s);i++)
	{
		int u=s[i]-'a';
		if(!ch[p][u]) ch[p][u]=++idx;
		p=ch[p][u];
	}
	val[p]=1;
}
inline void mark(char s[])
{
	int p=0;
	for(int i=0;i<strlen(s);i++)
	{
		int u=s[i]-'a';
		p=ch[p][u];
		vis[p]=1;
	}
}


inline void dfs(int now)
{
	if(val[now])
	{
		ans[++cnt]='P';
	}
	int flag=-1;
	for(int i=0;i<26;i++)
	{
		int ver=ch[now][i];
		if(!ver) continue;
		if(vis[ver])
		{
			flag=i;
		}
		else
		{
			ans[++cnt]=i+'a';
			dfs(ver);
		}
	}
	if(flag!=-1)
	{
		ans[++cnt]=flag+'a';
		dfs(ch[now][flag]);
	}
	if(flag==-1&&vis[now])
	{
		f=1;
	}
	if(!f) ans[++cnt]='-';
}
int main()
{
	scanf("%d",&n);
	int tt;
	for(int i=0;i<n;i++)
	{
		scanf("%s",s[i]);
		int len=strlen(s[i]);
		if(len>maxx) maxx=len,tt=i;
		insert(s[i]);
	}
	mark(s[tt]);

	dfs(0);
	printf("%d\n",cnt);
	for(int i=1;i<=cnt;i++)
	{
		printf("%c\n",ans[i]);
	}
	return 0;
}

标签:IOI2008,Printer,int,题解,5000007,include,Type
From: https://www.cnblogs.com/watasky/p/16843776.html

相关文章

  • CSPS2022 题解
    T1容易想到枚举\(B,C\),然后\(A,D\)可以预处理,即对于\(i\)处理存在路径\(1\rightarrowj\rightarrowi\)中\(j\)的权值最大的,那么只需枚举\(B,C\)然后分别取最......
  • CSP-S 2022 T1题解
    题目描述:在一张图中找到能够到达的四个点,使之点权之和最大。先说说考场上的思路吧,要求不超过k次转车,其实就是要求长度不超过k。所以只需要找出这张图的全源最短路,然后建......
  • 解决swagger2 --> Illegal DefaultValue null for parameter type integer 保存问题
    在pmo.xml中加入两个依赖<!--增加两个配置--><dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>${swagger-mode......
  • 2021 ICPC EC Final B. Beautiful String 题解
    2021ICPCECFinalB.BeautifulString题解题意问给定字符串t的所有子串中形如"114514"分割方案之和。其中'1'、'4'、'5'表示某一字符串,且可重复。分析(暴力\(n^3\))......
  • typescript number 转 string(转)
    转自:number转string一、双点解析10..toString();二、括号先计算再转换(10).toString();三、加空串10+''转自:number转string......
  • acm22纳新题题解:D.喜子哥开空调
    喜子哥开空调Description喜子掌管着工作室的空调遥控器,他可以自由调整室内的温度,(这么牛?!我咋不信。。)但每个人最喜欢的室内温度不都相同,如果当前室温k与自身适应的......
  • CSP-S 2022 Unofficial 题解
    去年有个CSP-S2021Unofficial的题解但是那玩意咕掉了(主要是不想写最后一题,但是准备省选的时候会补上毕竟ZJ卷怪一堆懂得都懂),不过今年保证在NOIP2022前会写完这份题......
  • [Typescript] 81. Medium - Number Range
    Sometimeswewanttolimittherangeofnumbers...Forexamples.typeresult=NumberRange<2,9>//|2|3|4|5|6|7|8|9 /*_____________Your......
  • [Typescript] 80. Medium - Construct Tuple
    Constructatuplewithagivenlength.Forexampletyperesult=ConstructTuple<2>//expecttobe[unknown,unkonwn] /*_____________YourCodeHere______......
  • typescript文件导入svelte文件报错处理办法
    在typescripts文件中引入svelte文件时报错Cannotfindmodule'../components/HelloWorld.svelte'oritscorrespondingtypedeclarations.ts(2307)并且svelte已安装......