字符串分类 - 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进制, ...
随机推荐
- 带你认识“货真价实”的P2P网贷风控
文/杨帆 说起P2P,多数金融圈内人士已经并不陌生.国内现有近千家的P2P网贷平台,动辄打出高息诱人的收益率宣传口号以及眼花缭乱的安全承诺.但是在这些浮华表面的背后,关于P2P的风控很多人仍然是一 ...
- Python--day36--操作系统的作用;多道技术;
- 2018-2-13-WPF-拖动时出现-Invalid-FORMATETC-structure
title author date CreateTime categories WPF 拖动时出现 Invalid FORMATETC structure lindexi 2018-2-13 17:2 ...
- win10 uwp 发布旁加载自动更新
在很多企业使用的程序都是不能通过微软商店发布,原因很多,其中我之前的团队开发了很久的应用,结果发现没有用户能从微软应用商店下载所以我对应用商店没有好感.但是作为一个微软粉丝,怎么能不支持 UWP 开发 ...
- vue 使用webpack打包后路径报错以及 alias 的使用
一.vue 使用webpack打包后路径报错(两步解决) 1. config文件夹 ==> index.js ==> 把assetsPublicPath的 '/ '改为 './' 2. b ...
- linux进程睡眠的介绍
对于一个进程"睡眠"意味着什么? 当一个进程被置为睡眠, 它被标识为处于一个特殊的状 态并且从调度器的运行队列中去除. 直到发生某些事情改变了那个状态, 这个进程将不被 在任何 C ...
- JQ ajaxFileUpload的一些问题
1.input之后没法再次获得响应事件,change无效 解决办法,对file这个Input的父级做响应事件. <div class="lineBox lineBox0_24 line ...
- 组合数学入门—TwelveFold Way
组合数学入门-TwelveFold Way 你需要解决\(12\)个组合计数问题. \(n\)个有标号/无标号的球分给\(m\)个有标号/无标号的盒子 盒子有三种限制: A.无限制 B.每个盒子至少有 ...
- 阿里云“网红"运维工程师白金:做一个平凡的圆梦人
他是阿里云的一位 P8 运维专家,却很有野心得给自己取花名“辟拾(P10)”:他没有华丽的履历,仅凭着 26 年的热爱与坚持,一步一个脚印踏出了属于自己的技术逆袭之路:他爱好清奇,练就了能在 20 秒 ...
- 【Kubernetes】架构全图
K8s是什么 Kubernetes是Google开源的容器集群管理系统.它构建在Docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩容缩容等一整套功能. K8s能做什么 ①容器的自 ...