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;
}

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3768704.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值