2019.5.11 提高B组 T2 nssl-1321 买门票

版权声明:虽然本蒟蒻很菜,但各位dalao转载请注明出处谢谢。 https://blog.csdn.net/xuxiayang/article/details/90111045

D e s c r i p t o i n Descriptoin

给定一个字符集,现在要用这些字符集拼凑单词

要求严格按照字典序且后面的字母的 A s c a l l Ascall 码比前面的大,并且至少有一个元音,两个辅音

15 , 26 拼凑字符集的长度\leq 15,字符集大小\leq 26


S o l u t i o n Solution

等价于求前 m i n { C n m , 25000 } min\{C_n^m,25000\} 的排列+一些特殊情况的判断

暴搜即可,时间复杂度: O ( m i n { C n m × n , 25000 n } ) O(min\{C_n^m\times n,25000n\})


C o d e Code

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;int L,C,c[27],now[16],Time;
char s[2];
bool v[27];
inline bool hf()//一些特判
{
	int yy=0;
	for(register int i=1;i<=L;i++) if(c[now[i]]+96=='a'||c[now[i]]+96=='e'||c[now[i]]+96=='i'||c[now[i]]+96=='o'||c[now[i]]+96=='u') yy++;
	return yy&&L-yy>1;
}
inline void dfs(int dep)
{
	if(dep>L)
	{
		if(!hf()) return;
		if(++Time>25000) return;
		for(register int i=1;i<=L;i++) putchar(c[now[i]]+96);
		putchar(10);
		return;
	}
	if(Time>25000) return;
	for(register int i=now[dep-1]+1;i<=C;i++)
	if(!v[i])
	{
		v[i]=true;now[dep]=i;
		dfs(dep+1);
		now[dep]=0;v[i]=false;
	}
}
inline long long read() 
{
	char c;long long d=1,f=0;
	while(!isdigit(c=getchar())) if(c=='-') d=-1;f=(f<<3)+(f<<1)+c-48;
	while(isdigit(c=getchar())) f=(f<<3)+(f<<1)+c-48;
	return d*f;
}
signed main()
{
	L=read();C=read();
	for(register int i=1;i<=C;i++) scanf("%s",s),c[i]=s[0]-96;
	sort(c+1,c+1+C);
	dfs(1);
}

猜你喜欢

转载自blog.csdn.net/xuxiayang/article/details/90111045
今日推荐