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

所有的测试数据都对,为什么还wrong answer 呢?

Posted by 123456789987654321 at 2008-09-29 11:00:21 on Problem 1256
#include<iostream>
using namespace std;
int an[20];
char str[20],b[20];
int compare(const void *e1,const void *e2)
{
	return *((int*)e1)-*((int*)e2);
}
void sort(char * a,char * b,int len)
{   
    int	i,j,k=1;
	int x[26],y[26];
	memset(x,0,sizeof(x));
	memset(y,0,sizeof(y));

	for(i=0;i<len;i++)
	{
		if(a[i]>='A'&&a[i]<='Z')
			x[a[i]-65]++;
		if(a[i]>='a'&&a[i]<='z')
			y[a[i]-97]++;
	}
	for(i=0;i<26;i++)
	{
		if(x[i]!=0)
		{
			for(j=1;j<=x[i];j++)
			 b[k++]=char(i+65);
		}
	    if(y[i]!=0)
		{
			for(j=1;j<=y[i];j++)
			 b[k++]=char(i+97);
		}
	}
	b[k]='\0';
}

int main()
{   
	freopen("in.txt","r",stdin);
	int len,i,j,k,m,n;

	scanf("%d",&m);
	while(m--)
	{
       cin>>str;
       len=strlen(str);
	   n=len;
	   sort(str,b,len);

       //an[0]=100;
	   an[1]=1;
       for(i=2;i<=len;i++)//映射成数字
	   {
		  if(b[i]!=b[i-1])
			  an[i]=an[i-1]+1;
		  else
			  an[i]=an[i-1];
	   }

      for(i=1;i<=len;i++) //输出第一个排列
		cout<<b[i];
	   cout<<endl;
    
	   for(i=2;i<len;i++)  //将b中重复的去掉
	   {
		   if(b[i]==b[i-1])
		   {
			   for(j=i;j<len;j++)
				   b[j]=b[j+1];
		   }
	   }

	   for(i=0;;i++)  //每次循环都找出下一个排列
	   {       
			for(j=n;j>=2&&an[j-1]>=an[j];j--);
			if(j>=2)
			{
				int nMinLarger=an[j];
				int nMinIdx=j;
				//下面找出从an[j]及其后最小的比an[j-1]大的元素,并记录其下标
				for(int kk=j;kk<=len;kk++)
					if(nMinLarger>an[kk]&&an[kk]>an[j-1])
					{
						nMinLarger=an[kk];
						nMinIdx=kk;
					}
					//交换位置
					an[nMinIdx]=an[j-1];
					an[j-1]=nMinLarger;
					qsort(an+j,n-j+1,sizeof(int),compare);
                    
	                for(k=1;k<=n;k++)
				    printf("%c",b[an[k]]);
					cout<<endl;
			}
			else
				break;
	   }
	}
	return 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