[恢]hdu 2072

2011-12-17 07:00:30

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072

题意:中文。统计“不同”单词。字符串处理。

代码:

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


char str[1010] ;
char words[1010][50] ;
int n ;



void getword(char str[])
{
int flag = 0 ;
int len = strlen(str) ;
int cnt = 0 ;
char word[50] ;
int i ;
str[len+1] = '\0' ;
str[len] = ' ';
n = 0 ;
for (i = 0 ; str[i] ; i++)
{
if (flag == 0)
{
if (str[i] != ' ')
{
word[cnt++] = str[i] ;
flag = 1 ;
}
}
else
{
if (str[i] == ' ')
{
word[cnt++] = '\0' ;
strcpy(words[n++], word) ;
cnt = 0 ;
flag = 0 ;
}
else
word[cnt++] = str[i] ;
}
}
}


int count()
{
int i, j, rtn = 0 ;
for (i = 0 ; i < n ; i++)
{
for (j = 0 ; j < i ; j++)
if (strcmp(words[i], words[j]) == 0)
break ;
if (j >= i) rtn++ ;
}
return rtn ;
}


int main ()
{
while (gets(str) && strcmp(str, "#"))
{
getword(str) ;
printf ("%d\n", count()) ;
}
return 0 ;
}



原文地址:https://www.cnblogs.com/lzsz1212/p/2315212.html