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

📄 2040266_wa.c

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

int B, L;
int D, M;
int maxlen;
int flag[10000];
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 = maxlen = 0;
	for(i = 0; i < D; i++)
	{
		scanf("%s",dic[i].phase);
		dic[i].numlen = strlen(dic[i].phase);
		if(dic[i].numlen>maxlen)
			maxlen = dic[i].numlen;
	}
}

int get_num()
{
	int i, j, ans, t, p, l;

	ans = 0;
	memset(flag,0,sizeof(flag));
	for(i = 0; i < D; i++)
	{
		t = 0;l = dic[i].numlen;p = 1;
		while(--l)
			p*=10;
		for(j = 0; j < dic[i].numlen; j++)
		{
			dic[i].num[j] = '0'+mark[dic[i].phase[j]-'A'];
			t += mark[dic[i].phase[j]-'A']*p;
			p /= 10;
		}
		if(flag[t]==0)
		{
			flag[t] = 1;
			ans++;
		}
		else
			if(flag[t]==1)
			{
				flag[t] = -1;
				ans--;
			}
	}
	return ans;
}

int get_num2()
{
	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;)
	{
		if(i<D-1&&strcmp(dic[i].num,dic[i+1].num)==0)
		{
			j = i+1;
			while(j<D&&strcmp(dic[i].num,dic[j].num)==0)
				j++;
			i = j;
		}
		else
		{
			ans++;
			i++;
		}
	}
	return ans;
}


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

	l = strlen(al);
	if(n==1)
	{
		for(i = 0; i < l; i++)
			mark[al[i]-'A'] = 1;
		if(maxlen > 3)
			t = get_num2();
		else
			t = get_num();
		if(t>M)
		{
			M = t;
			cpy();
		}
		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 + -