Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

自己写的递归,应该还能对大量重复优化,控制递归深度,但是不想搞了

Posted by Belldandy at 2012-07-21 16:41:47 on Problem 1731
#include<stdio.h>
struct NODE
{
	char c;
	int n;
}data[50];
char way[300];
int length=0,k=0;
void dfs(int n)
{
	int i=0;
	if(n==k-1)
	{
		for(i=0;i<length;i++)
		{
			if(data[i].n)
			{
				break;
			}
		}
		way[n]=data[i].c;
		for(i=0;i<k;i++)
		{
			printf("%c",way[i]);
		}
		printf("\n");
		return;
	}
	for(i=0;i<length;i++)
	{
		if(data[i].n)
		{
			way[n]=data[i].c;
			data[i].n--;
			dfs(n+1);
			data[i].n++;
		}
	}
	return;
}
int main()
{
	char temp[200]={0};
	int i=0,n=0,t[50]={0};
	scanf("%s",&temp);
	for(i=0;temp[i];i++)
	{
		t[temp[i]-'a']++;
		k++;
	}
	for(i=0;i<30;i++)
	{
		if(t[i])
		{
			data[length].c=i+'a';
			data[length++].n=t[i];
		}
	}
	dfs(0);
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator