lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题

水题

就是用来试试字符串算法的

tri树

  1. #include <map>
  2. #include <set>
  3. #include <cmath>
  4. #include <queue>
  5. #include <stack>
  6. #include <vector>
  7. #include <string>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <sstream>
  12. #include <iostream>
  13. #include <algorithm>
  14. #include <functional>
  15. #define For(i, x, y) for(int i=x; i<=y; i++)
  16. #define _For(i, x, y) for(int i=x; i>=y; i--)
  17. #define Mem(f, x) memset(f, x, sizeof(f))
  18. #define Sca(x) scanf("%d", &x)
  19. #define Scl(x) scanf("%lld",&x);
  20. #define Pri(x) printf("%d\n", x)
  21. #define Prl(x) printf("%lld\n",x);
  22. #define CLR(u) for(int i = 0; i <= N ; i ++) u[i].clear();
  23. #define LL long long
  24. #define ULL unsigned long long
  25. #define mp make_pair
  26. #define PII pair<int,int>
  27. #define PIL pair<int,long long>
  28. #define PLL pair<long long,long long>
  29. #define pb push_back
  30. #define fi first
  31. #define se second
  32. using namespace std;
  33. typedef vector<int> VI;
  34. const double eps = 1e-;
  35. const int maxn = 1e5 + ;
  36. const int INF = 0x3f3f3f3f;
  37. const int mod = 1e9 + ;
  38. inline int read()
  39. {
  40. int now=;register char c=getchar();
  41. for(;!isdigit(c);c=getchar());
  42. for(;isdigit(c);now=now*+c-'',c=getchar());
  43. return now;
  44. }
  45. int N,M;
  46. string s;
  47. char a[];
  48. int ans,tot;
  49. int tree[maxn][];
  50. int flag[maxn];
  51. void insert(char *str){
  52. int l = strlen(str);
  53. int root = ;
  54. for(int i = ; i < l; i ++){
  55. int id = str[i] - 'a';
  56. if(!tree[root][id]){
  57. tree[root][id] = ++tot;
  58. }
  59. root = tree[root][id];
  60. }
  61. if(!flag[root]){
  62. ans += flag[root] = ;
  63. }
  64. }
  65. int main()
  66. {
  67. while(getline(cin,s) && s[] != '#'){
  68. Mem(tree,);
  69. Mem(flag,);
  70. tot = ;
  71. ans = ;
  72. stringstream ss(s);
  73. while(ss >> a){
  74. insert(a);
  75. }
  76. Pri(ans);
  77. }
  78. return ;
  79. }

map/set

  1. #include <map>
  2. #include <set>
  3. #include <cmath>
  4. #include <queue>
  5. #include <stack>
  6. #include <vector>
  7. #include <string>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <sstream>
  12. #include <iostream>
  13. #include <algorithm>
  14. #include <functional>
  15. #define For(i, x, y) for(int i=x; i<=y; i++)
  16. #define _For(i, x, y) for(int i=x; i>=y; i--)
  17. #define Mem(f, x) memset(f, x, sizeof(f))
  18. #define Sca(x) scanf("%d", &x)
  19. #define Scl(x) scanf("%lld",&x);
  20. #define Pri(x) printf("%d\n", x)
  21. #define Prl(x) printf("%lld\n",x);
  22. #define CLR(u) for(int i = 0; i <= N ; i ++) u[i].clear();
  23. #define LL long long
  24. #define ULL unsigned long long
  25. #define mp make_pair
  26. #define PII pair<int,int>
  27. #define PIL pair<int,long long>
  28. #define PLL pair<long long,long long>
  29. #define pb push_back
  30. #define fi first
  31. #define se second
  32. using namespace std;
  33. typedef vector<int> VI;
  34. const double eps = 1e-;
  35. const int maxn = 1e5 + ;
  36. const int INF = 0x3f3f3f3f;
  37. const int mod = 1e9 + ;
  38. inline int read()
  39. {
  40. int now=;register char c=getchar();
  41. for(;!isdigit(c);c=getchar());
  42. for(;isdigit(c);now=now*+c-'',c=getchar());
  43. return now;
  44. }
  45. int N,M;
  46. string a,s;
  47. int ans = ;
  48. map<string,bool>P;
  49. int main()
  50. {
  51. while(getline(cin,s) && s[] != '#'){
  52. P.clear();
  53. ans = ;
  54. stringstream ss(s);
  55. while(ss >> a){
  56. if(!P[a]){
  57. ans++;
  58. P[a] = ;
  59. }
  60. }
  61. Pri(ans);
  62. }
  63. return ;
  64. }

Hash

  1. #include <map>
  2. #include <set>
  3. #include <cmath>
  4. #include <queue>
  5. #include <stack>
  6. #include <vector>
  7. #include <string>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <sstream>
  12. #include <iostream>
  13. #include <algorithm>
  14. #include <functional>
  15. using namespace std;
  16. const int MAXBUF=;char buf[MAXBUF],*ps=buf,*pe=buf+;
  17. inline bool isdigit(const char& n) {return (n>=''&&n<='');}
  18. inline void rnext(){if(++ps==pe)pe=(ps=buf)+fread(buf,sizeof(char),sizeof(buf)/sizeof(char),stdin);}
  19. template <class T> inline bool in(T &ans){
  20. #ifdef VSCode
  21. ans=;T f=;register char c;
  22. do{c=getchar();if ('-'==c)f=-;}while(!isdigit(c)&&c!=EOF);
  23. if(c==EOF)return false;do{ans=(ans<<)+(ans<<)+c-;
  24. c=getchar();}while(isdigit(c)&&c!=EOF);ans*=f;return true;
  25. #endif
  26. #ifndef VSCode
  27. ans =;T f=;if(ps==pe)return false;do{rnext();if('-'==*ps)f=-;}
  28. while(!isdigit(*ps)&&ps!=pe);if(ps==pe)return false;do{ans=(ans<<)+(ans<<)+*ps-;
  29. rnext();}while(isdigit(*ps)&&ps!=pe);ans*=f;return true;
  30. #endif
  31. }const int MAXOUT=; //*(int(*)[10])p
  32. char bufout[MAXOUT], outtmp[],*pout = bufout, *pend = bufout+MAXOUT;
  33. inline void write(){fwrite(bufout,sizeof(char),pout-bufout,stdout);pout = bufout;}
  34. inline void out_char(char c){*(pout++)=c;if(pout==pend)write();}
  35. inline void out_str(char *s){while(*s){*(pout++)=*(s++);if(pout==pend)write();}}
  36. template <class T>inline void out_int(T x) {if(!x){out_char('');return;}
  37. if(x<)x=-x,out_char('-');int len=;while(x){outtmp[len++]=x%+;x/=;}outtmp[len]=;
  38. for(int i=,j=len-;i<j;i++,j--) swap(outtmp[i],outtmp[j]);out_str(outtmp);}
  39. template<typename T, typename... T2>
  40. inline int in(T& value, T2&... value2) { in(value); return in(value2...); }
  41. #define For(i, x, y) for(int i=x;i<=y;i++)
  42. #define _For(i, x, y) for(int i=x;i>=y;i--)
  43. #define Mem(f, x) memset(f,x,sizeof(f))
  44. #define Sca(x) scanf("%d", &x)
  45. #define Scl(x) scanf("%lld",&x);
  46. #define Pri(x) printf("%d\n", x)
  47. #define Prl(x) printf("%lld\n",x);
  48. #define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
  49. #define LL long long
  50. #define ULL unsigned long long
  51. #define mp make_pair
  52. #define PII pair<int,int>
  53. #define PIL pair<int,long long>
  54. #define PLL pair<long long,long long>
  55. #define pb push_back
  56. #define fi first
  57. #define se second
  58. #define Vec Point
  59. typedef vector<int> VI;
  60. const double eps = 1e-;
  61. const int maxn = 1e6 + ;
  62. const int INF = 0x3f3f3f3f;
  63. const int mod = 1e9 + ;
  64. int N,M,tmp,K;
  65. char str[maxn];
  66. string s;
  67. vector<ULL> P;
  68. ULL Hash(char *p)
  69. {
  70. ULL h = ;
  71. ULL g;
  72. for (; *p; p++)
  73. {
  74. h = (h << ) + *p;
  75. g = h & 0xF0000000;
  76. if (g)
  77. {
  78. h ^= (g >> );
  79. h ^= g;
  80. }
  81. }
  82. return h;
  83. }
  84. int main()
  85. {
  86. while(getline(cin,s) && s[] != '#'){
  87. stringstream ss(s); P.clear();
  88. while(ss >> str) P.pb(Hash(str));
  89. sort(P.begin(),P.end());
  90. P.erase(unique(P.begin(),P.end()),P.end());
  91. Pri(P.size());
  92. }
  93. #ifdef VSCode
  94. write();
  95. system("pause");
  96. #endif
  97. return ;
  98. }

HDU2072 tri树/map/set/字符串hash的更多相关文章

  1. cf580E. Kefa and Watch(线段树维护字符串hash)

    题意 $n$个数的序列,$m + k$种操作 1.$l , r, k$把$l - r$赋值为$k$ 2.$l, r, d$询问$l - r$是否有长度为$d$的循环节 Sol 首先有个神仙结论:若询问 ...

  2. hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)

    题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...

  3. 字符串Hash/树Hash学习笔记

    哈希 Tags:字符串 作业部落 评论地址 一.概述 百度百科: 散列表(Hash table/哈希表),是根据关键码值(Key value)而直接进行访问的数据结构. 哈希表常用于比较两个字符串是否 ...

  4. 字符串hash与字典树

    title: 字符串hash与字典树 date: 2018-08-01 22:05:29 tags: acm 算法 字符串 概述 这篇主要是关于字符串里的 字符串hash 和 字符串字典树,,两个都是 ...

  5. 线段树 + 字符串Hash - Codeforces 580E Kefa and Watch

    Kefa and Watch Problem's Link Mean: 给你一个长度为n的字符串s,有两种操作: 1 L R C : 把s[l,r]全部变为c; 2 L R d : 询问s[l,r]是 ...

  6. hdu 4821 字符串hash+map判重 String (长春市赛区I题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...

  7. (通俗易懂小白入门)字符串Hash+map判重——暴力且优雅

    字符串Hash 今天我们要讲解的是用于处理字符串匹配查重的一个算法,当我们处理一些问题如给出10000个字符串输出其中不同的个数,或者给一个长度100000的字符串,找出其中相同的字符串有多少个(这样 ...

  8. 字符串hash+回文树——hdu6599

    拖了很久才补的回文树,感觉网上的博客都是一个做法..回文树统计不同种类的回文串出现次数,然后用字符串hash来判每个回文子串是否符合要求 #include<bits/stdc++.h> u ...

  9. P7114 [NOIP2020] 字符串匹配 (字符串hash+树状数组)

    好多题解用的扩展KMP(没学过,所以不用这种方法). 我们按照题目要求记F(s)表示s串的权值,可以预处理出前缀权值(用于A)和后缀权值(用于C),枚举AB的长度i=2~n-1,不需要分开枚举,我们只 ...

随机推荐

  1. Beta项目展示

    Team C# 一.团队成员简介 杜正远,队长. 博客地址:http://www.cnblogs.com/kevindu/ 崔强,全职PM. 博客地址:http://www.cnblogs.com/m ...

  2. 《Linux内核分析》第七周学习笔记

    <Linux内核分析>第七周学习笔记 可执行程序的装载 郭垚 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/co ...

  3. SpringMVC-RESTRUL___CRUD知识点总结

    RESTful风格 <!-- 携带surveyId去后台 --><!-- RESTFUL风格:/xxx/23 --><!-- 接收方式:@PathVariable注解 - ...

  4. 微信小程序动态数据跑马灯组件编写

    开发必备:熟悉微信小程序组件开发 开发思路:如果只有一条数据,直接用css3关键帧动画:如果有多条数据,则在当前动画运动到一定时间的时候,将其数据替换掉,使之在视觉效果上有一个从下到上播放的状态.数组 ...

  5. 使用Java+Kotlin双语言的LeetCode刷题之路(三)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  6. 定义类型别名(typedef,using)

    说到类型别名,无非是给类型名(如int,char,float,double,bool)取一个比较有特殊含义的名字而已 最常用的关键莫过于 typedef 吧 typedef最常见的用法是与结构体str ...

  7. 被深信服上网行为管理器AC拒绝的操作如何正常访问

    1.管理员登入帐号 2.如下图,在菜单[实时状态]-[上网行为监控]中,搜索指定IP的行为记录,找到被拒绝的数据 3.如下图,在菜单[系统管理]-[全局排除地址]中,增加不过滤的地址并提交即可  

  8. MySQL乐观锁在分布式场景下的实践

    背景 在电商购物的场景下,当我们点击购物时,后端服务就会对相应的商品进行减库存操作.在单实例部署的情况,我们可以简单地使用JVM提供的锁机制对减库存操作进行加锁,防止多个用户同时点击购买后导致的库存不 ...

  9. Angular @的作用

    <!DOCTYPE html><html lang="zh-cn" ng-app="myApp"><head> <me ...

  10. 软件开的目录规范+sys,os,time模块

    —————————————————————————————————————————————————————————————————— start.py import sys,os # print(__ ...