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> ...
随机推荐
- 你好,C++(5)如何输出数据到屏幕、从屏幕输入数据与读写文件?
2.2 基本输入/输出流 听过HelloWorld.exe的自我介绍之后,大家已经知道了一个C++程序的任务就是描述数据和处理数据.这两大任务的对象都是数据,可现在的问题是,数据不可能无中生有地产生 ...
- vim插件和配置
vim插件和配置 插件 pathogen 可以方便地管理vim插件 在没有pathogen的情况下,vim插件的文件全部都放在.vim目录,卸载插件很麻烦,pathogen可以将不同的插件放在一个单独 ...
- Android学习----Android架构
android分为四个层,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心层.蓝色的代表java程序,黄色的代码为运行JAVA程序而实现的虚拟机,绿色部分为C/C++语言编写 ...
- Json处理函数json_encode json_decode
json_decode — 对 JSON 格式的字符串进行编码 mixed json_decode ( string $json [, bool $assoc = false [, int $dept ...
- centos 下搭建 php环境(1)
3.PHP的安装 安装GD库(让PHP支持GIF,PNG,JPEG) 首先下载 jpeg6,libpng,freetype 并安装模块 wget http://www.ijg.org/files/jp ...
- jquery-te 轻量级的编辑器
- JavaScript注意事项
var m = "false"; Boolean(m); // true而非 false ajax不能设置setRequestHeaders("Cookie", ...
- [Delphi]检查URL是否有效的函数
function CheckUrl(url: string): boolean; var hSession, hfile, hRequest: hInternet; dwindex, dwcodele ...
- Android 4.4 上实现透明导航栏和状态栏 Translucent system bar
Translucent system UI styling To get the most impact out of your content, you can now use new window ...
- POJ3083 Children of the Candy Corn(搜索)
题目链接. 题意: 先沿着左边的墙从 S 一直走,求到达 E 的步数. 再沿着右边的墙从 S 一直走,求到达 E 的步数. 最后求最短路. 分析: 最短路好办,关键是沿着墙走不太好想. 但只要弄懂如何 ...