zzuliOj 1178

徐来丶清风徐来 2017-07-26 04:47:08

题目
Description

统计一篇文章里不同单词的总数。

Input

有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。每篇文章的单词数小于1000,每个单词最多由30个字母组成。

Output

每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。

Sample Input

you are my friend
#
Sample Output

4
HINT

Source


我的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct word
{
char str[32];
} W;
int main()
{
W p[1001];
char str1[32];
int i,j,k;
int count = 0;
while(strcmp((gets(str1)),"#")!=0)
{
memset(p,'\0',sizeof(p));
i=0;
count=0;
k=0;
while(str1[i]!='\0')
{
j=0;
while(str1[i]==' ')
i++;
for( ; str1[i]!=' '&&str1[i]!='\0'; i++)
{
p[k].str[j++]=str1[i];
}
for(j=0; j<k; j++)
{
if(strcmp( p[k].str, p[j].str)==0)
{
count--;
break;

}
}
count++;
k++;
while(str1[i]==' ')
i++;

}
printf("%d\n",count);

}

return 0;
}
一直wrong answer。。。。帮帮忙吧。。。
...全文
586 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2017-07-27
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct word
{
    char str[32];
} W;


typedef struct words {
    char word[32];
    int cnt;
}WORD;

int get_words(W *word_list, int max_num, char *sentence);

static W p[1001];
static int gword_num;

int main()
{
    char str1[32];
    int count = 0;

    memset(p,'\0',sizeof(p));
    while(strcmp((gets(str1)), "#") != 0)
    {
        count = get_words(p, 1001, str1);
        /*
           i=0;
           count=0;
           k=0;
           while(str1[i]!='\0')
           {
           j=0;
           while(str1[i] == ' ')
           i++;
           for( ; str1[i] != ' ' && str1[i] !='\0'; i++)
           {
           p[k].str[j++]=str1[i];
           }
           for(j=0; j<k; j++)
           {
           if(strcmp( p[k].str, p[j].str)==0)
           {
           count--;
           break;

           }
           }
           count++;
           k++;
           while(str1[i]==' ')
           i++;

           }
           */

        printf("%d\n",count);
    }
    printf("%d\n",count);

    return 0;
}


int get_words(W *word_list, int max_num, char *sentence)
{
    int i = 0, j = 0, k;
    int flag = 0;
    char tmp[32];

    while (sentence[i] == ' ')
        i++;

    for ( ; sentence[i] && sentence[i] != '\n'; i++) {
        if (sentence[i] != ' ' && sentence[i] != ','
                && sentence[i] != '!' && sentence[i] != '.') {
            tmp[j++] = sentence[i];
        } else if (j >= 1) {
            tmp[j] = 0;
            for (k = 0; k < max_num && strlen(word_list[k].str) > 0; k++) {
                if (strcmp(word_list[k].str, tmp) == 0) {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0) {
                strcpy(word_list[k].str, tmp);
                gword_num++;
            }
            j = 0;
            flag = 0;
        }
    }
    if (j >= 1) {
        tmp[j] = 0;
    }
    for (k = 0; k < max_num && strlen(word_list[k].str) > 0; k++) {
        if (strcmp(word_list[k].str, tmp) == 0) {
            flag = 1;
            break;
        }
    }
    if (flag == 0) {
        strcpy(word_list[k].str, tmp);
        gword_num++;
    }

    return gword_num;
}
做了一个参考吧
赵4老师 2017-07-27
  • 打赏
  • 举报
回复
static W p[1001]; 在占用内存空间较大的局部数组声明的前面加static将其从堆栈数据段挪到全局数据段即可避开因局部数组大小超过默认堆栈大小1MB造成程序不能正常运行的问题。
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
static W p[1001]; 在占用内存空间较大的局部数组声明的前面加static将其从堆栈数据段挪到全局数据段即可避开因局部数组大小超过默认堆栈大小1MB造成程序不能正常运行的问题。
为啥不加个标点符号呢,但我能大概看懂了
  • 打赏
  • 举报
回复
发现问题了,谢谢你们,内存申请的不够
  • 打赏
  • 举报
回复
帮帮忙吧,实在看不出来才求助的

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧