Uva--10815 (字符串处理)

2014-05-31 19:38:36

题意 & 思路:以scanf(%c)的方式吃单词(虽然getline也可以,但scanf %c不容易出错),因为要输出不同的单词,而文章中的单词可能重复,那就空间换时间。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;

struct Word{
    char s[50];
};
Word word[500000];

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(){
    //freopen("in","r",stdin);
    int num = 0,cnt = 0,flag = 0;
    char sen;
    while(scanf("%c",&sen) == 1){
        if((sen >= 'A' && sen <= 'Z') || (sen >= 'a' && sen <= 'z')){
            word[num].s[cnt++] = sen;
            flag = 1;
        }
        //隐含着不为字母的意思
        else if(flag){
            word[num].s[cnt] = '\0';//单词结尾
            Lowcase(word[num].s,cnt);
            flag = 0;
            cnt = 0;
            ++num;//记录数下移
        }
    }
    sort(word,word + num,cmp);
    cout << word[0].s << endl;
    for(int i = 1; i < num; i++){
        if(strcmp(word[i].s,word[i - 1].s) != 0)
            cout << word[i].s << endl;
    }
    return 0;
}

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值