字符串分类 - 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进制, ...
随机推荐
- 配置gitignore后使其生效命令
改动过.gitignore文件之后,在repo的根目录下运行: git rm -r --cached . git add . 之后可以进行提交: git commit -m "fixed u ...
- 关于Spring JavaWeb工程中的ContextRefreshedEvent事件
在应用启动时,通常想在此时预加载一些资源,全局使用. Spring会在操作应用上下文时,使用ApplicationEventPublisher触发相关ApplicationContextEvent,我 ...
- 【mac】Mac 终端如何切换成管理员用户
方法1.打开终端输入 sudo su 然后回车 Password: ------(输入root密码即可) sh-3.2# -------- (输入执行的命令即可,例如 npm i -g np ...
- Roslyn 使用 Directory.Build.props 管理多个项目配置
在一些大项目需要很多独立的仓库来做,每个仓库之间都会有很多相同的配置,本文告诉大家如何通过 Directory.Build.props 管理多个项目配置 在我的 MVVM 框架需要三个不同的库,一个是 ...
- 不幸的是,我试图在Eclipse中安装Maven 1.5,但出现了以下错误:
Cannot complete the install because one or more required items could not be found. Software being in ...
- jquery ajax请求步骤
$.ajax({ type: "GET", url: "/alink-hq/checkCode", data: { "mobile": ph ...
- 闲来无事写一个jquery计算器,没有进行封装......
<!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 第二阶段:流程图:8.axure绘制简单业务流程图
元件库选择流程图 流程图示例 一个简单的注册练习 线上可以写字 作业
- iOS @property、@synthesize和@dynamic
@property @property的本质: @property = ivar(实例变量) + getter/setter(存取方法); 在正规的 Objective-C 编码风格中,存取方法有着严 ...
- 配置一个yum私有仓库
使用一台服务器配置私有仓库做yum源,本身使用file,客户端使用http连接 安装http服务: [root@ceph1 ~]# yum -y install httpd 修改配置文件 Docume ...