10815 - Andy's First Dictionary

这题用gets()是没法过的,大概花了我2小时时间,因为会出现这样的数据:比如上一行的单词没完整,接着下一行还有一部分,这样的话gets就无能为力了,只能用getchar一个一个读入判断。

在这里先佩服下刘汝佳大神让做题人wa的能力,,膜拜!


#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define N 1000100
char a[N][210] = {'\0'};
int cmp(const void *a, const void *b)
{
    return strcmp((char *)a, (char *)b);
}
int main()
{
    int cnt = 0;
    int len = 0;
    char s;
    while(1)
    {
        if((s=getchar())==EOF)
            break;
        if(isalpha(s))
            a[cnt][len++] = tolower(s);
        else
            if(isalpha(a[cnt][0]))
            {
                a[cnt++][len] = '\0';
                len = 0;
            }
    }
    qsort(a, cnt, sizeof(a[0]), cmp);
    for(int i = 0; i < cnt; i++)
    {
        if(strcmp(a[i], a[i + 1]))
            printf("%s\n", a[i]);
    }
    return 0;
}


下面是我辛辛苦苦用gets做的,wa了无数次

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 10000
char ans[N][300];
int cmp(const void *a, const void *b)
{
    return strcmp((char *)a,(char *)b);
}
int main()
{
    int cnt = 0;
    char a[300];
    while(gets(a)!=NULL)
    {
        int len = strlen(a);
        int s=-2,e=-2;
        if(isalpha(a[0]))
            s = -1;
        for(int i = 0; i < len; i++)
        {
            if(!isalpha(a[i]))
            {
                char tmp[300];
                if(s!=-2 && isalpha(a[i-1]))
                {
                    e = i;
                    for(int j = s+1; j < e; j++)
                        tmp[j-s-1] = tolower(a[j]);
                    tmp[e-s-1] = '\0';
                    int flag = 0;
                    for(int j = 0; j < cnt; j++)
                        if(strcmp(tmp, ans[j])==0)
                            flag = 1;
                    if(!flag)
                        {strcpy(ans[cnt], tmp);cnt++;}
                    e = -2; s = -2;
                }
                if(s==-2&&isalpha(a[i+1]))
                    s = i;
            }
        }
    }
    qsort(ans, cnt, sizeof(ans[0]), cmp);
    for(int i = 0; i < cnt; i++)
        printf("%s\n", ans[i]);

    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值