POJ 3865 - Database 字符串hash
【题意】
给一个字符串组成的矩阵,规模为n*m(n<=10000,m<=10),如果某两列中存在两行完全相同,则输出NO和两行行号和两列列号,否则输出YES
【题解】
因为m很小,所以对每一行枚举其中两个字符串,检查之前行中对应的两列里是否重复即可。但是如何判重。
一开始想的把字符串做成pair然后用map映射为行号,但是TLE。
后来想到用hash判重,可能是因为哈希函数不够好,还是TLE。。。
总之这道题卡了三个小时,一直TLE。
枚举每一列,对枚举到的那一列从小到大排序,然后找到相邻两个相等的,接着在相同行,不同列中寻找相等的字符串,如果存在表示找到解。复杂度是m^2*n*logn,处于可以接受的范围。
最后的方法在POJ上使用C++通过,但是G++却超时了。
曾经记得POJ有一道题是使用G++通过,C++超时,总之以后在POJ上超时了就两个编译器都试试。
在看别人的代码时,发现有人用了字符串hash读入:
typedef unsigned long long ULL;
ULL hash(char *s) {
ULL res = ;
for(int i = ; s[i]; ++i) {
res *= ;//质数
res += s[i];
}
return res;
}
这样做比较速度相当快,程序用时不到1s。
因为没有处理冲突,所以可以用这个方法水过去(笑)
代码:(没有用字符串hash)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<string>
#include<sstream>
#define eps 1e-9
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define MAXN 1005
#define MAXM 40005
#define INF 0x3fffffff
using namespace std;
typedef long long LL;
int i,j,k,n,m,x,T,ans,big,cas,num,len,nodenum,l;
bool flag;
char s[],t[];
int pre;
string y[][]; string ex;
struct node
{
string st;
int i;
} G[]; bool cmp(node a,node b)
{
return a.st<b.st;
} int main()
{
scanf("%d%d",&n,&m);
gets(s);
for (i=;i<=n;i++)//读入
{
gets(s);
len=strlen(s);
s[len]=',';
s[len+]='\0';
pre=;
num=;
for (j=;j<=len;j++)
{
if (s[j]==',')
{
s[j]='\0';
y[i][++num]=(s+pre); pre=j+;
}
}
} for (i=;i<=m;i++)//枚举列
{
for (j=;j<=n;j++)//把这一列复制一遍后排序
{
G[j].st=y[j][i];
G[j].i=j;
}
sort(G+,G++n,cmp); for (j=;j<=n;j++)//枚举行
{
for (k=j+; k<=n && G[k].st==G[j].st ;k++)//向下找与第j行相同的行
{
for (l=i+;l<=m;l++)//再枚举其他列,检查是否有重复
{
if ( y[ G[j].i ][l]==y[ G[k].i ][l] )//存在重复
{
printf("NO\n");
printf("%d %d\n",G[j].i,G[k].i);
printf("%d %d\n",i,l);
return ;
}
}
}
}
} printf("YES\n");
return ;
}
POJ 3865 - Database 字符串hash的更多相关文章
- Palindrome POJ - 3974 (字符串hash+二分)
Andy the smart computer science student was attending an algorithms class when the professor asked t ...
- POJ 3974 - Palindrome - [字符串hash+二分]
题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...
- POJ 1200 字符串HASH
题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...
- 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...
- POJ 1743 Musical Theme (字符串HASH+二分)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15900 Accepted: 5494 De ...
- poj 3461 字符串单串匹配--KMP或者字符串HASH
http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include ...
- 字符串hash - POJ 3461 Oulipo
Oulipo Problem's Link ---------------------------------------------------------------------------- M ...
- poj 1200字符串hash
题意:给出不同字符个数和子串长度,判断有多少个不同的子串 思路:字符串hash. 用字符串函数+map为什么会超时呢?? 代码: #include <iostream> #include ...
- POJ 3461 Oulipo(字符串hash)
题目链接 字符串hash判断字符串是否相等. code #include<cstdio> #include<algorithm> #include<cstring> ...
随机推荐
- ios 中获得应用程序名称和版本号
IOS程序中的应用名称和版本号在 info.plist 文件中存储着,要想在程序中获得需要使用 NSBundle 对象 下面是示例代码: NSBundle *bundle = [NSBundle ma ...
- 『重构--改善既有代码的设计』读书笔记----Replace Method with Method Object
有时候,当你遇到一个大型函数,里面的临时变量和参数多的让你觉得根本无法进行Extract Method.重构中也大力的推荐短小函数的好处,它所带来的解释性,复用性让你收益无穷.但如果你遇到上种情况,你 ...
- java使用json抛出org.apache.commons.lang.exception.NestableRuntimeException解决方案
出现这个问题,说明缺少jar包,将下面的jar引入即可 commons-beanutils-1.8.3 commons-lang-2.6 (注:导入最新的 3.1 版本会继续报如下错误) common ...
- FMDB警告Warning: there is at least one open result set around after performing的问题
FMDB操作sqlite的时候总是报警告Warning: there is at least one open result set around after performing,后来发现是执行查询 ...
- localStorage 的基本使用
① localstorage大小限制在500万字符左右,各个浏览器不一致② localstorage在隐私模式下不可读取③ localstorage本质是在读写文件,数据多的话会比较卡(firefox ...
- SASS type-of 函数
今儿写个type-of,算是备忘录吧. 1.number type-of(0) // number type-of(1px) // number 2.string type-of(a) // stri ...
- Nginx源码研究八:nginx监听socket实现流程
前面描述了nginx系统分析nginx的配置文件,初始化模块相关参数的过程,这里利用nginx监听socket的实现过程,做一次完整的回顾 1.首先,nginx启动的main函数中,会先初始化cycl ...
- 探究Android SQLite3多线程
最近做项目时在多线程读写数据库时抛出了异常,这自然是我对SQlite3有理解不到位的地方,所以事后仔细探究了一番. 关于getWriteableDataBase()和getReadableDataba ...
- 集合及特殊集合arrayList
1,运用集合 arrayList 首先复制Colections加 : 创建arrayList ar =new arrayList(); ArrayList具体提供的功能:属性 ...
- U盘安装CentOS7
1:U盘启动,进入安装界面,点击Tab键,修改最后一行如下: ...=initrd.img linux dd quiet 查看centos系统盘符,例如:sdb4 2:重启电脑,进入安装界面,点击Ta ...