【hash】Seek the Name, Seek the Fame

【哈希和哈希表】Seek the Name, Seek the Fame

题目描述

The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:

Step1. Connect the father‘s name and the mother‘s name, to a new string S. 
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).

Example: Father=‘ala‘, Mother=‘la‘, we have S = ‘ala‘+‘la‘ = ‘alala‘. Potential prefix-suffix strings of S are {‘a‘, ‘ala‘, ‘alala‘}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)

输入

The input contains a number of test cases. Each test case occupies a single line that contains the string S described above. 
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.

输出

For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby‘s name.

样例输入

ababcababababcabab
aaaaa

样例输出

2 4 9 18
1 2 3 4 5

【题意】:

对于一个字符串s,找出所有相同的前缀后缀长度.

【题解】:

利用hash的方法,直接取两端的值。

代码是自己刚学hash时写的,所以有点乱,代码是参考wsy的。

 1 #include<bits/stdc++.h>
 2 #define Mp make_pair
 3 #define F first
 4 #define S second
 5 using namespace std;
 6 const int N = 1e6+7;
 7 const int M1 = 1e9+7 , M2 = 1e9+9;
 8 typedef long long ll;
 9 typedef struct Node{
10     long long first ,second;
11
12     Node (){}
13     Node ( ll u,ll v){ first = u , second = v ;}
14 }PII;
15 //typedef pair<ll,ll> PII;
16
17 const PII base{M2,M1},p{M1,M2},One{1ll,1ll},Zero{0ll,0ll};
18
19 PII operator - (PII u,PII v){
20     return Node( (u.first-v.first+p.first)%p.first ,(u.second-v.second+p.second)%p.second );
21 }
22 PII operator * ( PII u , PII v ){
23     return Node( (u.first*v.first)%p.first , (u.second*v.second)%p.second );
24 }
25 PII operator + ( PII u , PII v ){
26     return Node( (u.first+v.first)%p.first , (u.second+v.second)%p.second );
27 }
28 PII operator + ( PII u , int v ){
29     return Node( (u.first+v)%p.first , (u.second+v)%p.second );
30 }
31 bool operator != ( PII u,PII v ){
32     return !( u.first == v.first && u.second == v.second );
33 }
34 bool operator == ( PII u,PII v ){
35     return ( u.first == v.first && u.second == v.second );
36 }
37 PII Pow( PII a ,int b){
38     PII ans = One ;
39     while( b ){
40         if( b&1 )
41             ans = ans * a ;
42         b >>= 1 ;
43         a = a * a ;
44     }
45     return ans ;
46 }
47 PII sum[N];
48 char str[N];
49 int ans[N];
50 int main()
51 {
52     ios_base :: sync_with_stdio(0);
53     cin.tie(NULL),cout.tie(NULL);
54
55     while( cin >> str+1 ){
56         if( str[1] == ‘.‘) break;
57         int len = strlen(str+1);
58         int n = len ;
59         sum[n+1] = Zero ;
60         for(int i=len;i>=1;i--)
61             sum[i] = sum[i+1] * base + str[i] ;
62         int cnt = 0 ;
63         /*
64         for(int i=1;i<=n;i++){
65             printf("%lld %lld \n",sum[i].first,sum[i].second);
66         }
67         */
68         PII P = base ;
69         for(int i=n-1;i>=1;i--){
70             //printf("####   %d \n",i);
71             if( sum[1] - sum[1+i] == P * sum[n-i+1] ){
72                 ans[cnt++] = n-i ;
73             }
74             P = P * base ;
75             //printf("%lld * %lld = %lld \n ",sum[i].first,Pow(base,n-i-1).first ,(sum[n]-sum[n-i]).first );
76         }
77         //sort( ans , ans + cnt );
78         for(int i=0;i<cnt;i++){
79             cout << ans[i] << ‘ ‘;
80         }
81         cout << n << endl;
82         //cout << ans << endl ;
83     }
84     return 0;
85 }
86  

双哈希

原文地址:https://www.cnblogs.com/Osea/p/11333540.html

时间: 2024-08-26 02:41:28

【hash】Seek the Name, Seek the Fame的相关文章

POJ2299 Ultra-QuickSort 【树状数组】+【hash】

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39529   Accepted: 14250 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

【hash】BZOJ3751-[NOIP2014]解方程

[题目大意] 已知多项式方程:a0+a1*x+a2*x^2+...+an*x^n=0.求这个方程在[1,m]内的整数解(n和m均为正整数). [思路] *当年考场上怒打300+行高精度,然而没骗到多少orz 然而正解只有60+行 [前铺]f(n) mod p=f(n mod p) mod p 取四个素数,分别对每个ai取模.先预处理x=0..p-1的情况,直接代入多项式计算即可.再在O(m)时间内检验1..m,对于≥p的利用前铺公式可得.如果模四个素数结果均能得到0,说明这个数是方程的解. P.

【bzoj1941】 Sdoi2010—Hide and Seek

http://www.lydsy.com/JudgeOnline/problem.php?id=1941 (题目链接) 题意 给出n个二维平面上的点,求一点使到最远点的距离-最近点的距离最小. Solution KDtree板子,早就听jump说KDtree都是板子题→_→ 枚举点,求其最远点距离和最近点距离,更新答案.最远邻近域搜索跟最近差不多,就是把估价函数改一下. 细节 码农题注意细节 代码 // bzoj1941 #include<algorithm> #include<iost

【bzoj1941】[Sdoi2010]Hide and Seek KD-tree

题目描述 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他决定和他的好朋友giPi(鸡皮)玩一个更加寂寞的游戏---捉迷藏. 但是,他们觉得,玩普通的捉迷藏没什么意思,还是不够寂寞,于是,他们决定玩寂寞无比的螃蟹版捉迷藏,顾名思义,就是说他们在玩游戏的时候只能沿水平或垂直方向走.一番寂寞的剪刀石头布后,他们决定iPig去捉giPi.由于他们都很熟悉PKU的地形了,所以giPi只会躲在PKU内n个隐秘地点,显然iPig也只会

hdu 5183 Negative and Positive (NP)(STL-集合【HASH】)

题意: When given an array (a0,a1,a2,?an−1) and an integer K, you are expected to judge whether there is a pair (i,j)(0≤i≤j<n) which makes that NP−sum(i,j) equals to K true. Here NP−sum(i,j)=ai−ai+1+ai+2+?+(−1)j−iaj 1≤n≤1000000,−1000000000≤ai≤1000000000

[HDOJ 5183] Negative and Positive (NP) 【Hash】

题目链接:HDOJ - 5183 题目分析 分两种情况,奇数位正偶数位负或者相反. 从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中. BestCoder比赛的时候我写了 STL map, 然后TLE... 注意: Hash负数的时候 % 了一个质数,得到的是负数还要 + Mod !! 代码 #include <iostream> #include <cstdio> #include <cstdlib> #includ

BZOJ3555 [Ctsc2014]企鹅QQ 【hash】

题目 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大的互联网功能体验,满足用户对社交.资讯.娱乐.交易等多方面的需求. 小Q是PenguinQQ网站的管理员,他最近在进行一项有趣的研究--哪些账户是同一个人注册的.经过长时间的分析,小Q发现同一个人注册的账户名称总是很相似的,例如Penguin1,Penguin2,Penguin3--于是小Q决定先对这种相似的情形进行

bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列【hash】

我%&&--&()&%????? 双模hashWA,unsigned long longAC,而且必须判断hash出来的数不能为0???? 我可能学了假的hash 这个题求个前缀和,然后目标是找到距离当前位置最远,且能使这两个数组差分后2-k位相同 hash把差分后数组的2到k位压起来即可,用map存这个hash值最早出现的位置 但是我还是不明白为啥hash值不能为0啊?? #include<iostream> #include<cstdio> #i

【HASH】【UVA 10125】 Sumset

传送门 Description 给定一个整数集合S,求一个最大的d,满足a+b+c=d,其中a,b,c,d∈S Input 多组数据,每组数据包括: 第一行一个整数n,代表元素个数 下面n行每行一个整数,代表集合元素 输入结束的标志为n=0. Output 对于每组数据,输出: 一行,如果有解,输出一个整数,代表最大的d:否则输出no solution Sample Input 5 2 3 5 7 12 5 2 16 64 256 1024 0 Sample Output 12 no solut