hdu 2072(字典树模板,set,map均可做)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input 有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。 Output 每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。 Sample Input
you are my friend
#
Sample Output
4
set做法:
#include<iostream>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
typedef long long ll;
set<string>se;
int main()
{
string s;
while(getline(cin,s))
{
if(s[]=='#')
break;
string m;
se.clear();
int len=s.length();
// cout<<"len: "<<len<<endl;
for(int i=;i<len;i++)
{
// cout<<i<<" "<<s[i]<<endl;
if(s[i]>='a'&&s[i]<='z')
{
int j;
m.clear();
for(j=i;j<len&&s[j]>='a'&&s[j]<='z';j++)
{
m+=s[j];
}
i=j;
se.insert(m);
}
}
cout<<se.size()<<endl;
}
return ;
}
istringstream 做法(具体的我看不太懂,反正可以去掉空格):
#include<iostream>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<set>
using namespace std;
typedef long long ll;
set<string>q;
int main()
{
string s;
while(getline(cin,s)){
if(s[]=='#')
break;
istringstream sss(s);
q.clear();
string ss;
while(sss>>ss){
q.insert(ss);
}
cout<<q.size()<<endl;
}
return ;
}
重点是字典树操作,由于对num数组的理解不透彻,写出了一堆BUG,自己实在不会变通啊。
这里的num数组记录的是:标记当前字符串结尾。比如qwe qw : 先是qwe可以得到: 1(q) 2(w) 3(e) 4
0 0 1
表示以e结尾的加入了
然后qw,k=2处,num=1,表示以w结尾的加入了。这就是两个单词。
上代码吧,开始看ac自动机了
#include<iostream>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
typedef long long ll;
#include<map>
const int maxn=1e4+;
int tr[maxn][];
int num[maxn];
string s;
string mid;
int k=;
int ans=;
int join()
{
int p=;
int len=mid.length();
int ok1=,ok2=;
for(int i=;i<len;i++)
{
int c=mid[i]-'a';
if(!tr[p][c])
{
tr[p][c]=k++;
}
p=tr[p][c];
}
if(!num[p]) //当前单词结尾没被标记,说明是一个新的单词。
{
num[p]=;
return ;
}
else
return ;
}
int main() //qq as a
{
while(getline(cin,s))
{
if(s[]=='#')
break;
k=;
ans=;
memset(tr,,sizeof(tr));
memset(num,,sizeof(num));
int len=s.length();
for(int i=;i<len;i++)
{
if(s[i]>='a'&&s[i]<='z')
{
mid.clear();
int j;
for(j=i;j<len&&s[j]>='a'&&s[j]<='z';j++)
{
mid+=s[j];
}
i=j;
//
if(join()==)
{
ans++;
// cout<<mid<<" "<<ans<<endl;
}
}
}
cout<<ans<<endl;
}
return ;
}
hdu 2072(字典树模板,set,map均可做)的更多相关文章
- HDU - 1251 字典树模板题
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- hdu 1251 字典树模板题 ---多串 查找单词出现次数
这道题题目里没有给定数据范围 我开了2005 疯狂的WA 然后开了50000, A掉 我以为自己模板理解错 然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ ...
- 字典树模板题(统计难题 HDU - 1251)
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
- 字典树模板 HDU - 1251
题意: 给一些单词,换行键后,查找以后输入的单词作为前缀的话们在之前出现过几次. 思路: 字典树模板----像查字典的顺序一样 #include<string> #include<s ...
- hdu1521(字典树模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 中文题诶~ 思路: 字典树模板 代码1: 动态内存, 比较好理解一点, 不过速度略慢, ...
- CH 1601 - 前缀统计 - [字典树模板题]
题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...
- HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...
- 【统计难题】【HDU - 1251】【map打表或字典树】【字典树模板】
思路 题意:题目为中文题,这里不再过多阐述. 思路1:可以在读入单词表的过程中将单词分解,用map将它一 一记录 思路2:利用字典树,这个方法较快些,下面代码中会分别给出数组和结构体指针两种形式的字典 ...
- P1184 高手之在一起(字典树模板题,hash算法, map)
哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...
随机推荐
- C# Winform使用线程,委托定时更新界面UI控件,解决界面卡顿问题(转载)
一.定时执行主界面控件值 1.利用定时器 Thread t = null; private void InitTSJK() { t = new Thread(new ThreadStart(GetDa ...
- LeetCode1005 K次取反后最大化的数组和(贪心+Java简单排序)
题目: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次.(我们可以多次选择同一个索引 i.) 以这种方式修 ...
- P1006 换个格式输出整数
这道题相较于上一题来说就简单了许多.看题. 怎么感觉这道题有点类似P1002写出这个数.流程差不多,思路大致是先求出每一位上的数,然后根据 百十个 的顺序输出结果.题目比较简单,不做赘述,贴代码 代码 ...
- 永久免费云服务器搭建国内Moon服务加速ZeroTier
ZeroTier One本身的服务器都在国外访问速度很慢.可以通过搭建国内Moon服务加速解决连接慢的问题. 但是需要有固定外网IP的服务器,可以注册sanfengyun账号申请免费云服务器. 下面是 ...
- JNI调用so动态库
1.编写native接口 package org.demo; public class JniDemo { public static native int bmp2fea(byte[] bmp, b ...
- Elasticsearch 更新文档
章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...
- [ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: URLError: <urlopen error [Errno 10061] Connection refused>
[ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: URLError: <urlopen error ...
- 批量go get 代码
批量 go get 代码 Go
- tornado反向解析
tornado反向解析 在路由中添加name属性,并且不能使用元组路由,应当由tornado.web.url定义路由. app = tornado.web.Application([ (r'/', I ...
- POJ 2187:Beauty Contest 求给定一些点集里最远的两个点距离
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 31414 Accepted: 9749 D ...