字符串分类 - hash
链接:https://www.nowcoder.com/acm/contest/141/E
来源:牛客网
题目描述
1. For each i in [0,|S|-1], let Si be the substring of S starting from i-th character to the end followed by the substring of first i characters of S. Index of string starts from 0.
2. Group up all the Si. Si and Sj will be the same group if and only if Si=Sj.
3. For each group, let Lj be the list of index i in non-decreasing order of Si in this group.
4. Sort all the Lj by lexicographical order.
Eddy can't find any efficient way to compute the final result. As one of his best friend, you come to help him compute the answer!
输入描述:
Input contains only one line consisting of a string S. 1≤ |S|≤ 10
6
S only contains lowercase English letters(i.e.
).
输出描述:
First, output one line containing an integer K indicating the number of lists.
For each following K lines, output each list in lexicographical order.
For each list, output its length followed by the indexes in it separated by a single space.
输出
8
1 0
1 1
1 2
1 3
1 4
1 5
1 6
1 7 题意 : 给一个字符串,要求每个位置为开始的子串会有多少种不同的情况,将不同的情况分类,按类输出
思路分析:
将字符串延长一倍,预处理一遍 hash 值,任意一个子串的 hash 值就可以 O(1)的得到了,然后将 hash 值相同的串分类输出即可
代码示例:
using namespace std;
#define ll unsigned long long
const ll maxn = 1e6+5;
typedef pair<ll, ll>pa; char s[maxn*2];
ll len, len2;
ll p = 19873;
ll hash_[maxn*2]; ll pp[maxn];
void init(){
//printf("-------------------\n");
pp[0] = 1;
for(ll i = 1; i <= len; i++){
pp[i] = pp[i-1]*p;
//printf("------ %llu \n", pp[i]);
} } void gethash(){ for(ll i = 1; i <= len2; i++){
hash_[i] = hash_[i-1]*p+(s[i]-'a');
}
}
vector<ll>ve[maxn];
pa pre[maxn];
pa arr[maxn]; int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout); scanf("%s", s+1);
len = strlen(s+1);
init();
for(ll i = 1; i <= len; i++) s[i+len] = s[i];
len2 = len*2; gethash();
ll k = 1;
for(ll i = len; i < len2; i++){
ll num = hash_[i]-hash_[i-len]*pp[len]; pre[k++] = make_pair(num, i-len);
}
sort(pre+1, pre+1+len);
pre[k] = make_pair(-1, 0);
k = 1;
for(int i = 1; i <= len; i++){
ve[k].push_back(pre[i].second);
arr[k] = make_pair(ve[k][0], k);
while(pre[i+1].first == pre[i].first){
ve[k].push_back(pre[i+1].second);
i++;
}
k++;
}
sort(arr+1, arr+k);
printf("%llu\n", k-1); for(ll i = 1; i < k; i++){
ll x = arr[i].second;
printf("%llu ", ve[x].size());
for(ll j = 0; j < ve[x].size(); j++)
printf("%llu%c", ve[x][j], j==ve[x].size()-1?'\n':' ');
}
return 0;
}
字符串分类 - hash的更多相关文章
- redis 2 字符串 和 hash
string是最简单的类型,一个key对应一个value,string类型是二进制安全的.redis的string可以包含任何数据,比如JPG图片或者序列化的对象 操作 set 设置key ...
- mycat1.6.5分片(字符串拆分hash)
https://blog.csdn.net/webnum/article/details/78313525 分片规则:字符串拆分hash 一.conf/schema.xml文件 <?xm ...
- [转] POJ字符串分类
POJ 1002 - 487-3279(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=1002题意:略解法:二叉查找数,map,快排... POJ 1 ...
- Contest 20140914 Mushroom写情书 字符串雙hash 後綴數組
0111:Mushroom写情书 查看 提交 统计 提问 总时间限制: 10000ms 内存限制: 256000kB 描述 有一天,Mushroom准备向他的GF表白,为了增加表白成功率,Mush ...
- 字符串编码---hash函数的应用
之前就听说过有个叫做hash表的东西,这段时间在上信息论与编码,也接触了一些关于编码的概念,直到今天做百度之星的初赛的d题时,才第一次开始学并用hash 一开始我用的是mutimap和mutiset, ...
- 字符串算法hash
思路:给字符串做一个映射,两个元素相同,则他们的hash值必定相同. 注意:hash表必须是unsigned int类型,保证每个映射都是正数. 例题: Description 给出两个字符串W和T, ...
- 字符串,hash
字符串1.有序的字符的集合,不可变2.s.swapcase() 大变小,小变大3.s.capitalize() 第一个大写4.s.casefold() 返回将字符串中所有大写字符转换为小写后生成的字符 ...
- 字符串的hash匹配
如果要比较字符串是否相等,首先想到的是KMP算法,但是hash更加简洁易于编写,hash的目的是把一串字符转化成一个数字,用数字来比较是否相等.我让mod=912837417 Base=127,防止h ...
- POJ 1200 Crazy Search 字符串的Hash查找
第一次涉及HASH查找的知识 对于字符串的查找有很多前人开发出来的HASH函数,比较常用的好像是ELF 和 BKDR. 这道题没想到突破点是在于其nc值,告诉你组成字符串的字母种类. 还有用26进制, ...
随机推荐
- 2018-8-10-C#-代码占用的空间
title author date CreateTime categories C# 代码占用的空间 lindexi 2018-08-10 19:16:52 +0800 2018-2-13 17:23 ...
- H3C 出站包过滤工作流程
- 总结:关于留学网站使用laravel框架的总结
1.从git库中clone后本地项目根目录没有vendor文件夹,安装composer 2.composer install 报错 ,删除 composer.lock 文件,重新执行 composer ...
- 2018-8-10-WPF-程序生成类库错误
title author date CreateTime categories WPF 程序生成类库错误 lindexi 2018-08-10 19:16:53 +0800 2018-2-13 17: ...
- C# 循环的判断会进来几次
最近有小伙伴告诉我,在循环的判断条件只会计算一次,本金鱼不相信,于是就做了测试,本文记录我做的测试. 先来写一个简单的代码, 就一个循环,循环的判断是从一个函数获取值 class Program { ...
- Scala中的函数表达式
最近看Spark的东西,由于之前没有接触过lambda函数表达式,所以搜了点资料,特地纪录在此 Scala中的Lambda表达式 在函数式编程中,函数是基本的构造块.Scala融合了java中的面向对 ...
- dotnet 判断程序当前使用管理员运行降低权使用普通权限运行
有一些程序是不想通过管理员权限运行的,因为在很多文件的读写,如果用了管理员权限程序写入的程序,其他普通权限的程序是无法直接访问的.本文告诉大家如何判断当前的程序是通过管理员权限运行,然后通过资源管理器 ...
- 数据库java包
package com.hgkj.model.dao.impl; import java.sql.*; public class DBManager { private static final St ...
- 螺旋矩阵O(1)根据坐标求值
传送门 洛谷2239 •题意 从矩阵的左上角(第11行第11列)出发,初始时向右移动: 如果前方是未曾经过的格子,则继续前进,否则右转: 重复上述操作直至经过矩阵中所有格子. 根据经过顺序,在格子中依 ...
- boostrap-非常好用但是容易让人忽略的地方【5】:input-group-btn
1.正常的使用 <div class="form-group"> <div class="input-group"> <input ...