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

📄 pku2408.cpp

📁 这是ACM 方面的资料 是PKU的 北京大学的出来的
💻 CPP
字号:
#include <stdio.h>
#include <algorithm>
#include <string>
#include <string.h>
#define L 50
using namespace std;

typedef struct 
{
	char sw[L], sv[L];
	int v;
} Node;

const int size = 30001;
Node word[size];
Node ot[size];
int cnt, ct;

int cp1(const void *a, const void *b)
{
	int cv, cw;
	Node *aa = (Node *)a;
	Node *bb = (Node *)b;
	cw = strcmp(aa->sw, bb->sw);
	cv = strcmp(aa->sv, bb->sv);
	if (cv)
	{
		return cv;
	}
	else
	{
		return cw;
	}
}

void Count()
{
	int i, j, k;
	ot[0].v = 0;
	ot[0] = word[0];
	for (i = 0, j = 0, k = 0; i < cnt; i++)
	{
		if (!strcmp(word[i].sv, word[j].sv))
		{
			ot[k].v++;
		}
		else
		{
			j = i;
			i--;
			k++;
			ot[k].v = 0;
			ot[k] = word[j];
		}
	}
	ct = k + 1;
}

int cp2(const void *a, const void *b)
{
	int cv, cw, cf;
	Node *aa = (Node *)a;
	Node *bb = (Node *)b;
	cw = strcmp(aa->sw, bb->sw);
	if (aa->v != bb->v)
	{
		return bb->v - aa->v;
	}
	else
	{
		return cw;
	}
}

void OutPut()
{
	int i, j;
	for (j = 0; j < ct && j < 5; j++)
	{
		printf("Group of size %d: ", ot[j].v);
		for (i = 0; i < cnt; i++)
		{
			if (!strcmp(word[i].sv, ot[j].sv) && (i == 0 || strcmp(word[i].sw, word[i - 1].sw)))
			{
				printf("%s ", word[i].sw);
			}
		}
		printf(".\n");
	}
}

int main()
{
	int i;
	cnt = 0;
	memset(word, 0, sizeof(word));
	while (scanf("%s", word[cnt].sw) != -1)
		cnt++;
	for (i = 0; i < cnt; i++)
	{
		strcpy(word[i].sv, word[i].sw);
		sort(word[i].sv, word[i].sv + strlen(word[i].sv));
	}
	qsort(word, cnt, sizeof(word[0]), cp1);
	Count();
	qsort(ot, ct, sizeof(word[0]), cp2);
	OutPut();
	return 0;
}

⌨️ 快捷键说明

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