链接:https://www.nowcoder.com/acm/contest/141/E
来源:牛客网

题目描述

Eddy likes to play with string which is a sequence of characters. One day, Eddy has played with a string S for a long time and wonders how could make it more enjoyable. Eddy comes up with following procedure:

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.
示例1

输入

复制

abab

输出

复制

2
2 0 2
2 1 3
示例2

输入

复制

deadbeef

输出

复制

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的更多相关文章

  1. redis 2 字符串 和 hash

    string是最简单的类型,一个key对应一个value,string类型是二进制安全的.redis的string可以包含任何数据,比如JPG图片或者序列化的对象 操作    set    设置key ...

  2. mycat1.6.5分片(字符串拆分hash)

    https://blog.csdn.net/webnum/article/details/78313525   分片规则:字符串拆分hash 一.conf/schema.xml文件   <?xm ...

  3. [转] POJ字符串分类

    POJ 1002 - 487-3279(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=1002题意:略解法:二叉查找数,map,快排... POJ 1 ...

  4. Contest 20140914 Mushroom写情书 字符串雙hash 後綴數組

    0111:Mushroom写情书 查看 提交 统计 提问 总时间限制:  10000ms 内存限制:  256000kB 描述 有一天,Mushroom准备向他的GF表白,为了增加表白成功率,Mush ...

  5. 字符串编码---hash函数的应用

    之前就听说过有个叫做hash表的东西,这段时间在上信息论与编码,也接触了一些关于编码的概念,直到今天做百度之星的初赛的d题时,才第一次开始学并用hash 一开始我用的是mutimap和mutiset, ...

  6. 字符串算法hash

    思路:给字符串做一个映射,两个元素相同,则他们的hash值必定相同. 注意:hash表必须是unsigned int类型,保证每个映射都是正数. 例题: Description 给出两个字符串W和T, ...

  7. 字符串,hash

    字符串1.有序的字符的集合,不可变2.s.swapcase() 大变小,小变大3.s.capitalize() 第一个大写4.s.casefold() 返回将字符串中所有大写字符转换为小写后生成的字符 ...

  8. 字符串的hash匹配

    如果要比较字符串是否相等,首先想到的是KMP算法,但是hash更加简洁易于编写,hash的目的是把一串字符转化成一个数字,用数字来比较是否相等.我让mod=912837417 Base=127,防止h ...

  9. POJ 1200 Crazy Search 字符串的Hash查找

    第一次涉及HASH查找的知识 对于字符串的查找有很多前人开发出来的HASH函数,比较常用的好像是ELF 和 BKDR. 这道题没想到突破点是在于其nc值,告诉你组成字符串的字母种类. 还有用26进制, ...

随机推荐

  1. CentOS7在防火墙与端口上的操作

    https://jingyan.baidu.com/article/cdddd41cb3bf6c53cb00e1ac.html CentOS7在安装软件包或类库的时候,常常会因为防火墙的拦截和端口未开 ...

  2. canvas.toDataURL 由于跨域报错的解决方法

    关于canvas.toDataURL 由于跨域报错的解决方法 用过canvas,都知道toDataURL这个方法真好用,不仅合成图片用到它,压缩图片也用到它.但有一个问题,就是图片源不能跨域,不然会报 ...

  3. while循环&CPU占用率高问题深入分析与解决方案

    直接上一个工作中碰到的问题,另外一个系统开启多线程调用我这边的接口,然后我这边会开启多线程批量查询第三方接口并且返回给调用方.使用的是两三年前别人遗留下来的方法,放到线上后发现确实是可以正常取到结果, ...

  4. POJ 3660 Cow Contest(floyed运用)

    Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...

  5. H3C查看历史命令--用户以上

    [H3Cwang]display history-command   sys   sysname H3C   sysname H3Cwang   display history-command   q ...

  6. 【t068】智慧碑

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] DIABLO魔王和Mini都有三种属性,体力点,攻击点,以及集气点. 两人的攻击方式是这样的:采用回合 ...

  7. JS异步解决方案

    前言 异步最早的解决方案是回调函数,如ajax,事件的回调,setInterval/setTimeout中的回调.但是回调函数有回调地狱的问题; 为了解决回调地狱的问题,社区提出了Promise解决方 ...

  8. .net core 读取Excal文件数据及注意事项

    添加ExcelDataReader.DataSet引用. 调用下列方法: public class XlsHelper { public static System.Data.DataSet GetX ...

  9. 0008 CSS初识(行内式、内部样式表、外部样式表)

    typora-copy-images-to: media 第01阶段.前端基础.CSS初识 CSS层叠样式表 学习目标 理解 css的目的作用 css的三种引入方式 应用 css三种引用方式的书写 通 ...

  10. redis 和 memcached 有什么区别?redis 的线程模型是什么?为什么 redis 单线程却能支撑高并发?

    redis 和 memcached 有啥区别? redis 支持复杂的数据结构 redis 相比 memcached 来说,拥有更多的数据结构,能支持更丰富的数据操作.如果需要缓存能够支持更复杂的结构 ...