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 ...
随机推荐
- 150723培训心得(queue)
queue(STL中函数,就是指队列) #include <iostream> #include <queue> using namespace std; //这 ...
- 【cl】eclipse配置svn
查看Eclipse版本号 http://jingyan.baidu.com/article/020278118660e81bcd9ce545.html Window>preferences输入S ...
- IOS 动态库问答
- 【字符串处理算法】字符串包括的算法设计及C代码实现
一.需求描写叙述 给定一个长字符串和一个短字符串.编敲代码推断短字符串中的全部字符是否都在长字符串中.假设是,则长字符串包括短字符串:反之,不包括. 为了尽量包括大多数情况,字符串中能够包括大写和小写 ...
- 监控sql运行时剩余时间
--监控sql执行时剩余时间 你知道正在执行的sql大概须要多长时间么? 你知道正在执行的sql大概完毕了百分之几么? 你知道正在执行的sql大概还要多长时间完毕么? V$SESSION_LONGOP ...
- DBS-PowerDesginer:PowerDesigner最基础的使用方法入门学习
ylbtech-DBS-PowerDesginer:PowerDesigner最基础的使用方法入门学习 1.返回顶部 1. 1:入门级使用PowerDesigner软件创建数据库(直接上图怎么创建,其 ...
- Reorder List 最典型的linkedlist题目
https://oj.leetcode.com/problems/reorder-list/ Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder ...
- number随时间随机递增每天 不同 php(原创)
<?php namespace Controller; class IndexController{ public function getAutoRegUserName(){ $sql=&qu ...
- WinSocket聊天程序实例(多线程)
#pragma comment(lib,"Ws2_32.lib") #include <stdio.h> #include <Winsock2.h> SOC ...
- wolf
package com.wh.Demo50; /** * @author 王恒 * @datetime 2017年4月7日 下午4:40:54 * @description * 分析:子类重写父类的方 ...