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

分享个dfs解法 据说都是用map 我怎么就是想到dfs

Posted by 450415941 at 2012-07-15 21:08:51 on Problem 1318
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;

#define N 105
#define M 10
char map[N][M];
char mark[N];
char s[N],ans[M];
int n,len;
bool vis[M],flag;

void dfs(int step){
    int i,j;
    if(step == len){
        ans[len] = '\0';
        for(j = 0; j < n; ++j){
            if(!strcmp(map[j],ans) && !mark[j]){
                mark[j] = 1;
                printf("%s\n",map[j]);
                flag = 1;
                return ;
            }
        }
    }
    for(i = 0; i < len; ++i){
        if(!vis[i]){
            vis[i] = 1;
            ans[step] = s[i];
            dfs(step+1);
            vis[i] = 0;
        }
    }
}

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

int main(){
    n = 0;
    while(scanf("%s",s)!=EOF){
        if(!strcmp("XXXXXX",s))
            break;
        else
            strcpy(map[n++],s); 
    }
    while(scanf("%s",s)!=EOF){
        if(!strcmp("XXXXXX",s))
            break;
        len = strlen(s);
        qsort(s,len,sizeof(s[0]),cmp);
        memset(vis,0,sizeof(vis));
        memset(mark,0,sizeof(mark));
        flag = 0;
        dfs(0);
        if(!flag)
            printf("NOT A VALID WORD\n");
        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