您的位置:首页 > 其它

HDOJ   2072   单词数

2015-12-18 18:07 330 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2072

C语言的方式,gets()读入空格

#include<stdio.h>

#include<string.h>

#define N 10000

char article
,tmp[101],word
[101];

int main()

{

int
len,pos,k,cnt;


while(gets(article)!=NULL)

{


if(article[0]=='#') break;


cnt=0;


len=strlen(article);


pos=0;


while(pos<len)


{


sscanf(article+pos,"%s",tmp);


for(k=0;k<cnt;k++)


if(strcmp(word[k],tmp)==0)


break;


if(k==cnt)


strcpy(word[cnt++],tmp);


pos+=strlen(tmp)+1;


}


printf("%d\n",cnt);

}return
0;

}

************************************************

c++使用set挺给力

#include <set>

#include <string>

#include <iostream>

using namespace std;

int main(void)

{

set
<string> st;

string s =
"";

char c;

while ((c
= cin.get()) != '#')

{


s += c;


while (c != '\n')


{


while ((c = cin.get()) != ' ' && c
!= '\n')


s += c;


if (s.length()) st.insert(s);


s = "";


}


cout << st.size()
<< endl;


st.clear();

}

return
0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐