Uva--156 (检索,排序)

2014-06-04 19:47:37

题意&思路:基本的单词检索,排序和标记

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000;

struct Word{
    char s[25];
    char sorted[25];
}word[maxn + 5];

bool cmp(Word a,Word b){
    return strcmp(a.s,b.s) < 0;
}

void Lowcase(char *p,int len){
    for(int i = 0; i < len; ++i){
        if(p[i] >= 'A' && p[i] <= 'Z'){
            p[i] += ('a' - 'A');
        }
    }
}

int main(){
    int hash[maxn + 5];
    memset(hash,0,sizeof(hash));
    char c,t[25],num = 0,cnt = 0,flag = 0;
    while(scanf("%c",&c) == 1){
        if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
            t[cnt++] = c;
            flag = 1;
            //printf("%c",c);
        }
        else if(flag == 1){
            t[cnt] = '\0';
            strcpy(word[num].s,t);
            Lowcase(t,cnt);
            sort(t,t + cnt);
            strcpy(word[num].sorted,t);
            ++num;
            cnt = 0;
            flag = 0;
        }
        if(c == '#')
            break;
    }
    sort(word,word + num,cmp);
    for(int i = 0; i < num; ++i){    
        if(!hash[i])
            for(int j = i + 1; j < num; ++j){
                if(strcmp(word[i].s,word[j].s) != 0 
                        && strcmp(word[i].sorted,word[j].sorted) == 0){
                    hash[i] = hash[j] = 1;
                }
            }
        if(!hash[i]){
            printf("%s\n",word[i].s);
        }
    }
    return 0;
}

 

posted @ 2014-06-04 21:22  Naturain  阅读(108)  评论(0编辑  收藏  举报