UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm & String<Problem N>
N - 韩爷的梦
Time Limit: 200/100MS (Java/Others) Memory Limit: 1300/1300KB (Java/Others)
一天,韩爷去百度面试,面试官给了他这么一个问题。
给你2万个字符串,每个字符串长度都是100,然后把2万个字符串丢入一个 set< string >g 中,问最终set里含有多少个元素?
g 是一个用来存储字符串、具有去重功能的容器,即相同字符串在 g 中只能保留一个。
两个字符串相等,当且仅当,长度一样且对应位置的字符都一样。
韩爷前晚没睡好,随手写了一个程序交给面试官,然后就gg了。
#include<iostream>
#include<string>
#include<set>
using namespace std;
string s;
set<string>g;
int main(){
for(int k=1;k<=20000;k++){
cin>>s;
g.insert(s);
}
cout<<g.size()<<endl;
return 0;
}
韩爷醒来之后,发现这只是一个梦(还好只是个梦)。他回忆起梦中的面试官给他的内存限制和时间限制非常低,这么做肯定过不了,那么,现在你不在梦中,你能解决这个问题么?
Input
单case
每个case有且只有2万行,每一行包含一个字符串,每行字符串的长度都为100 (样例除外)
字符集:大写英文字母(A-Z),小写英文字母(a-z),数字(0-9)
Output
输出一个整数,表示最终set里含有多少个元素。
Sample input and output
| Sample Input | Sample Output |
|---|---|
aaAa |
4 |
Hint
样例只是样例,不在test中
注意时间限制和内存限制非常低
解题报告:
直接上哈希即可,双哈希单哈希都可以过,如果过不了,请换素数。。。。囧
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 2e4 + 100;
char str[maxn]; void hashinit(int &x1,int &x2)
{
x1 = 0x7FED7FED , x2 = 1;
int p1 = 1526597;
int p2 = 89834777;
int mod1 = 1e9 + 7;
int mod2 = 1e9 + 9;
unsigned int x3 = 0x23322322;
for(int i = 1 ; i <= 100 ; ++ i)
{
int val = str[i];
x1 = (x1*p1 + val)%mod1;
}
for(int i = 1 ; i <= 100 ; ++ i)
{
int val = str[i];
x2 = (x2*p2 + val)%mod2;
}
} int main(int argc,char *argv[])
{
int hash1[maxn];
int hash2[maxn];
int size = 0;
for(int i = 1 ; i <= 20000 ; ++ i)
{
scanf("%s",str+1);
int q1,q2;
hashinit(q1,q2);
hash1[size] = q1,
hash2[size++] = q2;
}
sort(hash1,hash1+size);
sort(hash2,hash2+size);
int c1 = unique(hash1,hash1+size) - hash1;
int c2 = unique(hash2,hash2+size) - hash2;
printf("%d\n",min(c1,c2));
return 0;
}
UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm & String<Problem N>的更多相关文章
- UESTC_基爷的中位数 2015 UESTC Training for Search Algorithm & String<Problem D>
D - 基爷的中位数 Time Limit: 5000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_邱老师降临小行星 2015 UESTC Training for Search Algorithm & String<Problem B>
B - 邱老师降临小行星 Time Limit: 10000/5000MS (Java/Others) Memory Limit: 65536/65535KB (Java/Others) Su ...
- UESTC_基爷与加法等式 2015 UESTC Training for Search Algorithm & String<Problem C>
C - 基爷与加法等式 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm & String<Problem K>
K - 秋实大哥の恋爱物语 Time Limit: 5000/2000MS (Java/Others) Memory Limit: 32000/32000KB (Java/Others) Su ...
- UESTC_全都是秋实大哥 2015 UESTC Training for Search Algorithm & String<Problem J>
J - 全都是秋实大哥 Time Limit: 5000/2000MS (Java/Others) Memory Limit: 32000/32000KB (Java/Others) Subm ...
- UESTC_吴队长征婚 2015 UESTC Training for Search Algorithm & String<Problem E>
E - 吴队长征婚 Time Limit: 10000/4000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_王之迷宫 2015 UESTC Training for Search Algorithm & String<Problem A>
A - 王之迷宫 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- UESTC_Palindromic String 2015 UESTC Training for Search Algorithm & String<Problem M>
M - Palindromic String Time Limit: 3000/1000MS (Java/Others) Memory Limit: 128000/128000KB (Java ...
- UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>
L - Ferris Wheel String Time Limit: 3000/1000MS (Java/Others) Memory Limit: 43000/43000KB (Java/ ...
随机推荐
- mysql5.5 无法创建实例,error 16001
今天想用jdbc做个小程序,结果发现好久不用的mysql不好用了,我装的是社区版(win7)环境下,按理说不可能出问题,找了一堆解决方案都没解决,准备重装的时候想把mysql服务停了,直接在dos输入 ...
- 怎样合并排序数组(How to merge 2 sorted arrays?)
Question: We have 2 sorted arrays and we want to combine them into a single sorted array. Input: arr ...
- dig命令(转载)
dig命令使用大全(linux上域名查询) 可以这样说,翻译本篇文档的过程就是我重新学习DNS的过程,dig命令可以帮助我们学习DNS的原理,配置,以及其查询过程.以前使用dig仅仅是查询一下A记录或 ...
- poj1330Nearest Common Ancestors(LCA小结)
题目请戳这里 题目大意:意如其名. 题目分析:本题只有一个查询,所以可以各种乱搞过去. 不过对于菜鸟而言,还是老老实实练习一下LCA算法. LCA有很多经典的算法.按工作方式分在线和离线2种. tar ...
- Git 笔记一 Git简介
git 笔记一 什么是版本控制 所谓版本控制就是记录对文件的修改记录,这样以后就能回退到需要的 版本.比如你对一段代码进行了几次修改,有几次修改不想要了,如果 使用了版本控制,就可以回退到未做这些修改 ...
- [Python学习笔记][第八章Python异常处理结构与程序调试]
1/30 第八章Python异常处理结构与程序调试 异常处理 try-except结构 try: try块 except Exception: except块 try-except-else结构 tr ...
- RadioButton 和 RadioButtonList 比较
第一次接触RadioBttonList时候,觉得这个控件完全可以取代RadioButton,操作更加简便.直到今天,完成了一个小小的功能,才发现,尺有所短不是瞎掰的. 需求如下: Add Prorat ...
- Javascript进阶篇——浏览器对象—Location、Navigator、userAgent、screen对象
Location对象location用于获取或设置窗体的URL,并且可以用于解析URL.语法: location.[属性|方法] location对象属性图示: location 对象属性: loca ...
- 8.0 BOM对象
主要的掌握的知识结构图 1 Window 2 控制窗口.框架.弹出窗口 3 利用location对象中的页面信息 4 使用 navigator 对象了解浏览器 1.1 BOM的核心对象是window, ...
- IOS 开发调试方法
0.警告 尽量一个警告都不要有 1.错误 1)红色提示 编译过不去的原因大部分是语法,检查括号的匹配,变量名称,作用域范围 2)编译可以通过,可以运行 a.运行过程中程序崩溃 在debug区域的右侧, ...