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 4053040 at 2010-01-31 01:36:19 on Problem 1256
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;

int cmp ( const void *a , const void *b ) 
{ 
	return *(int *)a - *(int *)b; 
}

void change(int &a, int &b)
{
	int t;
	t = a;
	a = b;
	b = t;
}

int factorial(int n)
{
	int sum = 1, i;
	for(i = 1; i <= n; i ++)
		sum *= i;
	return sum;
}


int main()
{
	//freopen("in.txt","r",stdin);
	int N;
	char letter[14];
	int gg[14];
	int i, len;
	scanf("%d", &N);
	while(N --) {
		while(scanf("%s", letter) != EOF) {
			len = strlen(letter);
			for(i = 0; i < len; i ++) {
				if(letter[i] >= 'A' && letter[i] <= 'Z')        //表明大写
					gg[i] = (letter[i] - 'A') * 2;
				else       //letter[] >= 'a' && letter[] <= 'z'
					gg[i] = (letter[i] - 'a') * 2 + 1;            // 为了产生这个顺序 'A'<'a'<'B'<'b'<...<'Z'<'z'.    //接下来把顺序枚举即可
			}

			qsort(gg,len,sizeof(gg[0]),cmp);          //从小到大排列

			for(i = 0; i < len; i ++) {
				if(gg[i] % 2 != 0) {      
					printf("%c", (gg[i] - 1) / 2 + 'a');  
				}
				else
					printf("%c", gg[i] / 2 + 'A');
			}
			cout << endl;

			int p;
			int MAX = 24;
			for(int k = 0; k < factorial(len) - 1; k ++) {		
				for(i = 0; i < len; i ++) {
					p = len - 1;
					if(gg[p] > gg[p - 1]) {
						change(gg[p], gg[p - 1]);
						for(i = 0; i < len; i ++) {
							if(gg[i] % 2 != 0) {      
								printf("%c", (gg[i] - 1) / 2 + 'a');  
							}
							else
								printf("%c", gg[i] / 2 + 'A');
						}
						printf("\n");
					}
					else {//a[p] < a[p - 1]
						//p --;
						while(gg[p] <= gg[p - 1]) {        //while(gg[p] < gg[p - 1]) 一个符号改了我1个半小时 天!!
							p --;	
						}
						if(p < 1) break;
						int min = 100;
						int tt = 0;
						int j;
						for(j = p; j < len; j ++) {
							if(gg[j] <= min && gg[j] > gg[p - 1]) {
								min = gg[j];
								tt = j;
							}
						}
						change(gg[p - 1], gg[tt]);
						qsort(gg + p, len - p, sizeof(int), cmp);
						for(i = 0; i < len; i ++) {
							if(gg[i] % 2 != 0) {      
								printf("%c", (gg[i] - 1) / 2 + 'a');  
							}
							else
								printf("%c", gg[i] / 2 + 'A');
						}
						printf("\n");		
					}
				}
			}
		}
	}
	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