(HDU)2072
来源:互联网 发布:淘宝助理批量修改价格 编辑:程序博客网 时间:2024/06/04 19:34
单词数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 50028 Accepted Submission(s): 12236
Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend#
Sample Output
4
Author
Lily
Source
浙江工业大学网络选拔赛
Recommend
linle
分析:这题其实可以不用字典树做,但是使用字典树做需要注意一些方面。关键位置看我的代码(注释位置是WA后才发现的)。
#include <bits/stdc++.h>using namespace std;struct node{ int next[26]; bool isword; void init() { isword=false; memset(next,-1,sizeof(next)); }}T[1000000];int tot,p,words;char txt[10000],temp[100];void insert(char* s){ int i,p=0,len=strlen(s); for(i=0;i<len;i++) { int x=s[i]-'a'; if(T[p].next[x]==-1) { T[tot].init(); T[p].next[x]=tot++; } p=T[p].next[x]; } if(!T[p].isword&&p) words++; //不加&&p 开头的空格也会算作一个词 T[p].isword=true;}int main(){ while(gets(txt)&&txt[0]!='#') { T[0].init(); tot=1; p=words=0; int len=strlen(txt); txt[len]=' '; //在原始文本串后加一个空格 txt[++len]='\0'; //保证最后一个词可以输入 for(int i=0;i<len;i++) { if(txt[i]==' ') { temp[p]='\0'; insert(temp); p=0; }else{ temp[p++]=txt[i]; } } printf("%d\n",words); } return 0;}
比较骚的套路(暴力出奇迹)
#include <stdio.h> #include <string.h> int main() { int n,i,j,k,o,sum,d; char a[1000],b[1000][100],c[1000][100]; while(gets(a)) { k=strlen(a); if(a[0]=='#') break; j=0; n=0; o=0; for(i=0;i<k;i++) { if(a[i]!=' ') { b[j][n]=a[i]; n++; o=1; } if(a[i]==' '&&o==1) { b[j][n]='\0'; j++;n=0;o=0; } } if(i>0&&a[i-1]!=' ') { b[j][n]='\0'; j++; } n=0; strcpy(c[0],b[0]); for(i=0;i<j;i++) { d=0; for(k=0;k<n;k++) { if(strcmp(c[k],b[i])==0) { d=1; break; } } if(d==0) { strcpy(c[n],b[i]); n++; } } printf("%d\n",n); } return 0; }
最骚的套路(SET大法好)
#include<stdio.h>#include<string.h>#include<set>#include<iostream>#include<string>using namespace std;int main(){char s[1000];set<string>S;while(gets(s)!=NULL&&s[0]!='#'){int j,k,count=0;int m=strlen(s);string b[100];char *p;p=strtok(s," ") ;for( k=0;p!=NULL;k++){ b[k]=p; //cout<<b[k]<<endl; p=strtok(NULL," ") ;} for(int i=0;i<k;i++){if(S.count(b[i])==0){S.insert(b[i]); }}int ans=S.size();printf("%d\n",ans);S.clear();}return 0;}//AC代码
0 0
- (HDU)2072
- hdu 2072 Java 单词数(超级坑) HDU 2072
- HDU 2072 (STL set)
- hdu 2072
- hdu 2072
- HDU 2072
- HDU-2072
- hdu 2072
- hdu 2072
- hdu 2072
- hdu 2072
- HDU 2072
- HDU 2072
- hdu 2072
- hdu 2072
- hdu 2072
- HDU 2072
- HDU 2072
- 一个菜鸟安装虚拟机的小结
- 4 数字加密 (15分)——一维数组
- XML文件编写自动提醒设置
- opencv-android笔记1:android studio 2.3 + opencv-android-sdk 实现 camera预览
- Javascript中三个常见问题(2)
- (HDU)2072
- C++之模板函数、类模板
- 爬虫编写经验
- java中volatile、synchronized和lock解析
- centos7 安装jdk8
- Spark架构简明分析
- JAVA为什么会空指针异常
- Convert image resource to drawable
- 数据库存储过程