ZOJ 3891 K-hash
K-hash
This problem will be judged on ZJU. Original ID: 3891
64-bit integer IO format: %lld Java class name: Main
K-hash is a simple string hash function. It encodes a string Sconsist of digit characters into a K-dimensional vector (h0, h1, h2,... , hK-1). If a nonnegative number x occurs in S, then we call x is S-holded. And hi is the number of nonnegative numbers which are S-holded and congruent with i modulo K, for i from 0 to K-1.
For example, S is "22014" and K=3. There are 12 nonnegative numbers are "22014"-holded: 0, 1, 2, 4, 14, 20, 22, 201, 220, 2014, 2201 and 22014. And three of them, 0, 201 and 22014, are congruent with 0 modulo K, so h0=3. Similarly, h1=5 (1, 4, 22, 220 and 2014 are congruent with 1 modulo 3), h2=4(2, 14, 20 and 2201 are congruent with 2 modulo 3). So the 3-hash of "22014" is (3, 5, 4).
Please calculate the K-hash value of the given string S.
Input
There are multiple cases. Each case is a string S and a integer number K. (S is a string consist of '0', '1', '2', ... , '9' , 0< |S| ≤ 50000, 0< K≤ 32)
Output
For each case, print K numbers (h0, h1, h2,... , hK-1 ) in one line.
Sample Input
123456789 10
10203040506007 13
12345678987654321 2
112123123412345123456123456712345678123456789 17
3333333333333333333333333333 11
Sample Output
0 1 2 3 4 5 6 7 8 9
3 5 5 4 3 2 8 3 5 4 2 8 4
68 77
57 58 59 53 49 57 60 55 51 45 59 55 53 49 56 42 57
14 0 0 14 0 0 0 0 0 0 0
Source
Author
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct SAM {
struct node {
int son[],f,len;
void init() {
f = -;
len = ;
memset(son,-,sizeof son);
}
} s[maxn<<];
int tot,last;
void init() {
tot = last = ;
s[tot++].init();
}
int newnode() {
s[tot].init();
return tot++;
}
void extend(int c){
int np = newnode(),p = last;
s[np].len = s[p].len + ;
while(p != - && s[p].son[c] == -){
s[p].son[c] = np;
p = s[p].f;
}
if(p == -) s[np].f = ;
else{
int q = s[p].son[c];
if(s[p].len + == s[q].len) s[np].f = q;
else{
int nq = newnode();
s[nq] = s[q];
s[nq].len = s[p].len + ;
s[q].f = s[np].f = nq;
while(p != - && s[p].son[c] == q){
s[p].son[c] = nq;
p = s[p].f;
}
}
}
last = np;
}
} sam;
queue<int>q;
int du[maxn],dp[maxn][],ans[maxn],k;
char str[maxn];
int main(){
while(~scanf("%s%d",str,&k)) {
memset(du,,sizeof du);
memset(ans,,sizeof ans);
sam.init();
for(int i = ; str[i]; ++i) sam.extend(str[i] - '');
for(int i = ; i < sam.tot; ++i) {
memset(dp[i],,sizeof dp[i]);
for(int j = ; j < ; ++j) if(sam.s[i].son[j] != -) ++du[sam.s[i].son[j]];
}
//cout<<du[0]<<endl;
while(!q.empty());
for(int i = ; i < sam.tot; ++i) if(!du[i]) q.push(i);
dp[][] = ;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = ; i < ; ++i) {
int v = sam.s[u].son[i];
if(v == -) continue;
if(--du[v] == ) q.push(v);
if(u == && i == ) continue;
for(int j = ; j < k; ++j)
dp[v][(j* + i)%k] += dp[u][j];
}
for(int i = ; i < k; ++i)
ans[i] += dp[u][i];
}
ans[]--;
for(int i = ; str[i]; ++i)
if(str[i] == '') {
ans[]++;
break;
}
for(int i = ; i < k-; ++i)
printf("%d ",ans[i]);
printf("%d\n",ans[k-]);
}
return ;
}
ZOJ 3891 K-hash的更多相关文章
- ZOJ 3632 K - Watermelon Full of Water 优先队列优化DP
K - Watermelon Full of Water Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld &am ...
- ZOJ 3599 K倍动态减法游戏
下面的文字辅助理解来自http://blog.csdn.net/tbl_123/article/details/24884861 博弈论中的 K倍动态减法游戏,难度较大,参看了好多资料才懵懂! 此题可 ...
- Java集合源码分析(七)HashMap<K, V>
一.HashMap概述 HashMap基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了不同步和允许使用 null 之外,HashMap ...
- POJ 1840 HASH
题目链接:http://poj.org/problem?id=1840 题意:公式a1x1^3+ a2x2^3+ a3x3^3+ a4x4^3+ a5x5^3=0,现在给定a1~a5,求有多少个(x1 ...
- js实现hash
由于项目中用到了hash,自己实现了一个. Hash = function () { } Hash.prototype = { constructor: Hash, add: function (k, ...
- C++ 简单 Hash容器的实现
主要实现了以整数为关键字的hash,以key%m_nSize为哈希函数,以(hash(key)+i)%m_nSize重新寻址,并附带了elf_hash的实现,使用过程中可灵活修改. #ifndef _ ...
- [poj3349]Snowflake Snow Snowflakes(hash)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 37615 Accepted: ...
- [Locked] Maximum Size Subarray Sum Equals k
Example 1: Given nums = [1, -1, 5, -2, 3], k = 3,return 4. (because the subarray [1, -1, 5, -2] sums ...
- HashMap之Hash碰撞冲突解决方案及未来改进
说明:参考网上的两篇文章做了简单的总结,以备后查(http://blogread.cn/it/article/7191?f=wb ,http://it.deepinmind.com/%E6%80%A ...
随机推荐
- 聚类k-means/k-means++/fcm学习笔记
聚类主要是一种无监督学习.用来发现未标注数据的隐藏结构,主要是用来给数据分组.聚类算法一般有硬聚类(k-means,k-means++)和软聚类FCM(fuzzy c-means).聚类眼下广泛应用于 ...
- java生成一张图片
public class CreateImage { public static void main(String[] args) throws Exception{ int width = 100; ...
- Java图形界面(GUI)——如何将JTable成功放入面板
在这次课程设计中,大部分内容都设计的很流畅,没有遇到太大的问题,但在面板中加入JTable时确实花费了一部分时间,在此将解决办法总结出来: 定义控件: JPanel panel = new JPane ...
- element-UI中table表格的row-click事件怎么获取一行数据的id
<el-table :data="tableData" style="width: 100%" @row-click="openDetails( ...
- APP_ABI
在Application.mk文件中有个预定义命令参数APP_ABI,是指明编译与调试的CPU架构. 目前Android系统支持以下七种不同的CPU架构:ARMv5,ARMv7(从2010年起),x8 ...
- Razor Intro
http://www.w3schools.com/aspnet/razor_intro.asp Razor is not a programming language. It's a server s ...
- Qt5.9 WebChannel
Qt WebChannel enables peer-to-peer communication between a server (QML/C++ application) and a client ...
- DevExpress的GridControl拖拽DraopDown后计算HitInfo的RowHandle错误
最近在使用GridControl的拖拽功能时候遇到了一个问题:当GridControl触发DropDrop事件时,计算对应的RowHandle错误.当把鼠标拖拽到GridView一个单元格的靠上面的部 ...
- week2 notebook2
Beginning day2: 1.基础数据类型宏观: 1.1.整型:int:1,2,3 1.2.字符串:str:‘anthony’ 1.2.1: 索引:索引即下标,就是字符串组成的元素从第一个开始, ...
- JavaScriptResult