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

递归,代码,此题坚决不用STL,那样太没意思

Posted by smilerxz at 2009-08-12 15:59:26 on Problem 1256 and last updated at 2009-08-12 16:01:24
In Reply To:Re:呵呵,递归108K 31MS ac 掉了 Posted by:calme at 2009-07-19 15:06:19
#include <iostream>
#include <algorithm>
using namespace std;

#define MAXN 13

char a[MAXN + 1];
int len;
char flag[MAXN];
char s[MAXN + 1];

bool cmp(char a, char b) {
    return tolower(a) == tolower(b) ? a < b : tolower(a) < tolower(b);
}

void solve(int n) {
    if (n == len) {
        puts(s);
        return;
    }
    for (int i = 0; i < len; i++) {
        if (! flag[i]) {
            flag[i] = 1;
            s[n] = a[i];
            solve(n + 1);
            flag[i] = 0;
            while (i + 1 < len && a[i + 1] == a[i]) i++;
        }
    }
}

int main() {
    int CASE;
    scanf("%d", &CASE);
    while (CASE--) {
        scanf("%s", a);
        len = strlen(a);
        sort(a, a + len, cmp);
        memset(flag, 0, sizeof(flag));
        s[len] = 0;
        solve(0);
    }
    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