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;
}
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;
}
相关文章推荐
- HDOJ&nbsp;&nbsp;2025&nbsp;&nbsp;&nbsp;查找最大元素
- HDOJ&nbsp;&nbsp;2024&nbsp;&nbsp;&nbsp;&nbsp;C语言合法标识符
- HDOJ&nbsp;&nbsp;2016&nbsp;&nbsp;&nbsp;数据的交换输出
- HDOJ&nbsp;&nbsp;2014&nbsp;&nbsp;&nbsp;&nbsp;青年歌手大奖赛_评…
- HDOJ&nbsp;&nbsp;&nbsp;2013&nbsp;&nbsp;&nbsp;&nbsp;蟠桃记
- HDOJ&nbsp;&nbsp;2012&nbsp;&nbsp;&nbsp;素数判定
- HDOJ&nbsp;&nbsp;&nbsp;2007&nbsp;&nbsp;&nbsp;平方和与立方和
- HDOJ&nbsp;&nbsp;2005&nbsp;&nbsp;&nbsp;第几天?
- HDOJ&nbsp;&nbsp;2004&nbsp;&nbsp;&nbsp;成绩转换
- HDOJ&nbsp;&nbsp;1019&nbsp;&nbsp;Least&nbsp;Common&nbsp;Multipl…
- HDOJ&nbsp;&nbsp;1017&nbsp;&nbsp;&nbsp;&nbsp;A&nbsp;Mathematical&nbsp;Cur…
- HDOJ&nbsp;&nbsp;1013&nbsp;&nbsp;&nbsp;Digital&nbsp;Roots
- HDOJ&nbsp;&nbsp;1012&nbsp;&nbsp;&nbsp;&nbsp;u&nbsp;Calculate&nbsp;e
- HDOJ&nbsp;&nbsp;1016&nbsp;&nbsp;&nbsp;Prime&nbsp;Ring&nbsp;Problem
- HDOJ&nbsp;&nbsp;2181&nbsp;&nbsp;&nbsp;哈密顿绕行世界问题
- HDOJ&nbsp;&nbsp;1181&nbsp;&nbsp;&nbsp;&nbsp;变形课
- 韦东山第三期视频监控wpa_supplicant编译出错问题
- Android图片裁剪开源组件 实现了图片翻转,裁剪
- 使用Android Studio新建项目
- 关于android进程间通信(handler、messenger、AIDL)