⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2017840_tle.c

📁 北大大牛代码 1240道题的原代码 超级权威
💻 C
字号:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>

int B, L;
int D, M;
int mark[27];
char alph[27] = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
int output[27];
struct node
{
	char phase[11];
	char num[27];
	int numlen;
}dic[1001];

int cmp(const void *a, const void *b)
{
	struct node *aa = (struct node *)a;
	struct node *bb = (struct node *)b;
	if(strlen(aa->num)==strlen(bb->num))
		return strcmp(aa->num,bb->num);
	else
		return strlen(aa->num)-strlen(bb->num);
}

void cpy()
{
	int i;

	for(i = 0; i < L; i++)
		output[i] = mark[i];
}

void input()
{
	int i;

	scanf("%d%d%d",&B,&L,&D);
	M = 0;
	for(i = 0; i < D; i++)
	{
		scanf("%s",dic[i].phase);
		dic[i].numlen = strlen(dic[i].phase);
	}
}

int get_num()
{
	int i, j, ans;

	ans = 0;
	for(i = 0; i < D; i++)
	{
		for(j = 0; j < dic[i].numlen; j++)
			dic[i].num[j] = '0'+mark[dic[i].phase[j]-'A'];
	}	
	qsort(dic,D,sizeof(dic[0]),cmp);
	for(i = 0; i < D; i++)
	{
		//puts(dic[i].phase);
		//puts(dic[i].num);
	}
	//system("pause");
	for(i = 0; i < D;)
	{
		if(i<D-1)
		{
			//puts(dic[i].num);
			//puts(dic[i+1].num);
			//system("pause");
		}
		if(i<D-1&&strcmp(dic[i].num,dic[i+1].num)==0)
		{
			//printf("Y\n");
			j = i+1;
			while(j<D&&strcmp(dic[i].num,dic[j].num)==0)
				j++;
			i = j;
		}
		else
		{
			ans++;
			i++;
		}
	}
	//printf("ans = %d\n",ans);
	return ans;
}

void enum_ans(char al[], int n)
{
	int i, l, t;

	//puts(al);
	//printf("n = %d\n",n);
	l = strlen(al);
	if(n==1)
	{
		for(i = 0; i < l; i++)
			mark[al[i]-'A'] = 1;
		//for(i = 0; i < 27; i++)
		//	printf("%d",mark[i]);
		//printf("\n");
		if((t=get_num())>M)
		{
			M = t;
			cpy();
		}
		//printf("t = %d\n",t);
		return ;
	}
	for(i = 0; i <= l-n; i++)
	{
		mark[al[i]-'A'] = n;
		enum_ans(&al[i+1],n-1);
	}
}

void print()
{
	int i, j;

	printf("%d\n",M);
	for(i = 0; i < L;)
	{
		printf("%c",i+'A');
		j = i+1;
		while(output[j]==output[i])
		{
			printf("%c",j+'A');
			j++;
		}
		i = j;
		printf("\n");
	}
}

int main()
{
	input();
	enum_ans(&alph[26-L],B);
	print();
	return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -