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

贴代码。。求救。。一直WA

Posted by Mrchen89 at 2010-08-24 14:37:53 on Problem 1035
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct trie{
    bool match;
    int index;
    trie *next[26];   
}root, tree[100000];
int ns, cnt;
char str[11010][50], word[50], ans[11010];
int l;
void insert(char s[]){
    trie *temp = &root;
    for (int j = 0; j < strlen(s); j++){
        if (temp->next[s[j]-'a'] == NULL){
            for (int i = 0; i < 26; i++)
                tree[ns].next[i] = NULL;
            tree[ns].index = 0;
            tree[ns].match = false;
            temp->next[s[j]-'a'] = &tree[ns++];
        }
        temp = temp->next[s[j]-'a'];
    }
    temp->match = true;
    temp->index = l;
    
}

int search(char s[]){
    trie *temp = &root;
    for (int i = 0; i < strlen(s); i++){
        if (temp->next[s[i]-'a'] == NULL)
            return -1;
        temp = temp->next[s[i]-'a']; 
    }
    if (temp->match)return temp->index;
    else return -1;
}

void add(char *s){
    char temp[50];
    for (int i = 0; i <= strlen(s); i++){
        for (int q = 'a'; q <= 'z'; q++){
            if (q == s[i])continue;    
            for (int j = 0; j < i; j++)
                temp[j] = s[j];
            temp[i] = q;
            for (int k = i; k < strlen(s); k++)
                temp[k+1] = s[k];
            temp[strlen(s)+1] = '\0';
            int t = search(temp);
            if (t >= 0){
                ans[cnt++] = t;   
            }
        }   
    } 
}

void del(char *s){
    char temp[50];
    for (int i = 0; i < strlen(s); i++){
        for (int j = 0; j < i; j++)
            temp[j] = s[j];
        for (int k = i+1; k < strlen(s); k++)
            temp[k-1] = s[k];
        temp[strlen(s)-1] = '\0';
        int t = search(temp);
        if (t >= 0){
            ans[cnt++] = t;
        }   
    }
}

void change(char *s){
    char temp[50];    
    strcpy(temp, s);
    for (int i = 0; i < strlen(temp); i++){
        for (int q = 'a'; q <= 'z'; q++){
            if (q == s[i])continue;    
            temp[i] = q;
            int t = search(temp);
            if (t >= 0){
                ans[cnt++] = t;
            }
            temp[i] = s[i];
        }      
    }
}

int main(){
    while (scanf("%s", str[l])){
        if (!strcmp(str[l],"#"))break;
        insert(str[l]);  
        l++;
    }       
    while (scanf("%s", word)){
        if (!strcmp(word, "#"))break;        
        if (search(word) >= 0){
            printf("%s is correct\n", word);
            continue;   
        }
        add(word);
        del(word);
        change(word);
        if (cnt){
            printf("%s: ", word);
            sort(ans, ans+cnt);
            for (int i = 0; i < cnt-1; i++)
                if (ans[i]!=ans[i+1])
                printf("%s ", str[ans[i]]); 
            printf("%s\n", str[ans[cnt-1]]);
            cnt = 0;
            memset(ans, 0, sizeof(ans));  
        }
        else{
            printf("%s:\n", word);
            cnt = 0;
            memset(ans, 0, sizeof(ans));   
        }
    }
    system("pause");
    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