K-hash

Time Limit: 2000ms
Memory Limit: 131072KB

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 (h0h1h2,... , 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 (h0h1h2,... , 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

ZHOU, Yuchen
 
解题:在SAM上dp
 
 #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的更多相关文章

  1. ZOJ 3632 K - Watermelon Full of Water 优先队列优化DP

    K - Watermelon Full of Water Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld &am ...

  2. ZOJ 3599 K倍动态减法游戏

    下面的文字辅助理解来自http://blog.csdn.net/tbl_123/article/details/24884861 博弈论中的 K倍动态减法游戏,难度较大,参看了好多资料才懵懂! 此题可 ...

  3. Java集合源码分析(七)HashMap<K, V>

    一.HashMap概述 HashMap基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了不同步和允许使用 null 之外,HashMap  ...

  4. POJ 1840 HASH

    题目链接:http://poj.org/problem?id=1840 题意:公式a1x1^3+ a2x2^3+ a3x3^3+ a4x4^3+ a5x5^3=0,现在给定a1~a5,求有多少个(x1 ...

  5. js实现hash

    由于项目中用到了hash,自己实现了一个. Hash = function () { } Hash.prototype = { constructor: Hash, add: function (k, ...

  6. C++ 简单 Hash容器的实现

    主要实现了以整数为关键字的hash,以key%m_nSize为哈希函数,以(hash(key)+i)%m_nSize重新寻址,并附带了elf_hash的实现,使用过程中可灵活修改. #ifndef _ ...

  7. [poj3349]Snowflake Snow Snowflakes(hash)

    Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 37615 Accepted: ...

  8. [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 ...

  9. HashMap之Hash碰撞冲突解决方案及未来改进

    说明:参考网上的两篇文章做了简单的总结,以备后查(http://blogread.cn/it/article/7191?f=wb  ,http://it.deepinmind.com/%E6%80%A ...

随机推荐

  1. hibernate之7.one2many双向

    表结构 实体类关系 实体类源代码 Student package com.demo.model; import java.io.UnsupportedEncodingException; import ...

  2. hdu1209(Clock)

    pid=1209">点击打开hdu1209 Problem Description There is an analog clock with two hands: an hour h ...

  3. typeof、instanceof、hasOwnProperty()、isPrototypeOf()

    typeof 操作符 instanceof 操作符 hasOwnProperty()方法 isPrototypeOf()方法 1.typeof 用于获取变量的类型,一般只返回以下几个值:string, ...

  4. [javascript] jQuery系列之目录汇总

    最近一个月写了些关于jQuery的文章,谢谢大家的支持.文章仅我个人观点,也许有不对的地方,请指出.这个系列还在更新中 一:jQuery基础系列: jQuery温习篇---强大的JQuery选择器 j ...

  5. Redis List 命令技巧

    1.实现栈的功能(先进后出) lpush + lpop = stack > lpush mylist (integer) > lpop mylist " > lpop my ...

  6. js原生淘宝京东宝贝放大镜效果

    js实现商城放大镜效果 效果: 鼠标放上去会有半透明遮罩.右边会有大图片局部图. 鼠标移动时右边的大图片也会局部移动. 技术点: Event Event 是一个事件对象,当一个事件发生后,和当前事件发 ...

  7. 实现第三方登录(QQ、微信、微博)

    第三方登录,就是使用大家比较熟悉的比如QQ.微信.微博等第三方软件登录自己的网站,这可以免去注册账号.快速留住用户的目的,免去了相对复杂的注册流程.下边就给大家讲一下怎么使用PHP开发QQ登录的功能. ...

  8. POJ 1654 乱搞题?

    题意: 从一个点出发,8个方向,给出每一步的方向,求出走过的路径形成的多边形的面积. 思路: 先普及一下向量叉乘.. (摘自度娘) 也就是x1y2-x2y1. 那这不就好说了嘛. 一个经过原点的闭合多 ...

  9. 洛谷P2455 [SDOI2006]线性方程组(高斯消元)

    题目描述 已知n元线性一次方程组. 其中:n<=50, 系数是[b][color=red]整数<=100(有负数),bi的值都是整数且<300(有负数)(特别感谢U14968 mmq ...

  10. VHDL之package

    Pacakge Frequently used pieces of VHDL code are usually written in the form of COMPONENTS, FUNCTIONS ...