hdu2651

/*
分析:
    水题,把每个读入的串,根据相邻字母重复次数,
构成一个新的串。
    我这个用了字典树,也可以不用,直接把所有构
成的串排一下序,然后就可以计算了。


                                                 2013-01-26
*/








#include"stdio.h"
#include"string.h"
#include"stdlib.h"

int n;
__int64 ans;
struct Dictree
{
	struct Dictree *child[9];
	int num;
};
struct Dictree *root;

void insert(char str[])
{
	struct Dictree *now,*next;
	int i,l;
	now=root;
	for(i=0;str[i];i++)
	{
		if(now->child[str[i]-'0']!=NULL)
			now=now->child[str[i]-'0'];
		else
		{
			next=(struct Dictree *)malloc(sizeof(struct Dictree));
			next->num=0;
			for(l=1;l<=8;l++)	next->child[l]=NULL;
			now->child[str[i]-'0']=next;
			now=next;
		}
	}
	now->num++;
}
void get_ans(struct Dictree *now)
{
	int l;
	__int64 temp=now->num-1;
	ans+=(1+temp)*temp/2;
	for(l=1;l<=8;l++)
		if(now->child[l]!=NULL)
			get_ans(now->child[l]);
}
int main()
{
	int i,l;
	int temp,k;
	char str1[22],str2[22];
	while(scanf("%d",&n)!=-1)
	{
		root=(struct Dictree *)malloc(sizeof(struct Dictree));
		root->num=0;
		for(l=1;l<=8;l++)	root->child[l]=NULL;
		for(i=0;i<n;i++)
		{
			scanf("%s",str1);
			k=0;
			temp=1;
			for(l=1;str1[l];l++)
			{
				if(str1[l]!=str1[l-1])
				{
					str2[k++]=temp+'0';
					temp=1;
				}
				else	temp++;
			}
			str2[k++]=temp+'0';
			str2[k]=0;
			insert(str2);
		}
		ans=0;
		get_ans(root);
		printf("%I64d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值