您的位置:首页 > 其它

[热身题][hdoj_2072]单词数

2012-08-11 14:45 253 查看
// hdoj_2072 单词数
// 0MS	236K	695 B	GCC

#include <stdio.h>
#include <string.h>

char word[100000];
char arr[100][100];     //arr用于存储以前出现过的单词

int main(void)
{
int len, pos, count;
char temp[100];
while(gets(word) && strcmp(word, "#") != 0)
{
len = strlen(word);
pos = 0;
count = 0;
// pos加单词长度一直到>=len
while(pos < len)
{
sscanf(word + pos, "%s", temp); //把一个单词存入temp,空格忽略
int i;
for(i = 0; i < count; i ++)
if(strcmp(arr[i], temp) == 0)//如果和以前存入的单词相同,则不计数
break;
if(i == count)
strcpy(arr[count++], temp); //把temp存入arr,并计数器cnt加一
/*用pos来记录下次读取的位置*/
for(i = pos; word[i] == ' '; i++)	//空格
pos++;
pos += strlen(temp) + 1;	//单词
}
//判断是否全为空格
int k, m = 0;
for(k = 0; k < len; k ++)
if(word[k] == ' ')
m ++;
if(m == len)
printf("0\n");
else
printf("%d\n", count);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: