【hash】Power Strings】的更多相关文章

[题意]: 给出s串出来,能否找到一个前缀 ,通过多次前缀进行拼接.构成s串.如果有多个,请输出最多次数那个. 如:aaaa 可以用1个a,进行4次拼接 可以用2个a,进行2次拼接 可以用4个a,进行1次拼接 提供两种做法: 第一种是:利用kmp算法中求解  longest prefix table的方法, 找到最后一个位置的上的 p[len-1],如果用总长度减去(len-p[len-1])能否通过这一个前缀来拼接出来. 就是判断是否能整除. #include<bits/stdc++.h>…
Power Strings Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non…
Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative inte…
题目描述 给定若干个长度小于等于10^6的字符串,询问每个字符串最多由多少个相同的子串重复连接而成.如:ababab,最多由3个ab连接而成. 输入输出格式 输入格式 若干行,每行一个字符串. 当读入到“.”时结束程序. 输出格式 若干行,为对应的答案. 输入输出样例 输入样例 abcd aaaa ababab . 输出样例 1 4 3 题解 这道题可以用字符串hash或kmp来做. 主要就是要将这道题转换成求最长前缀满足同为后缀. 假设在s[1...n]中,s[1...i]为前缀且s[n-i+…
http://poj.org/problem?id=2406 (题目链接) 题意 给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的, 求 R 的最大值. Solution 后缀数组论文题,然而nlogn的倍增竟然TLE了,那给3s是什么意思→_→ 做法比较简单,穷举字符串 S 的长度 k,然后判断是否满足.判断的时候,先看字符串 L 的长度能否被 k 整除,再看 suffix(1)和 suffix(k+1)的最长公共前缀是否等于 n-k.在询问最长公共前缀的时候,suf…
题意: n<=1000000,cas较大 思路:这是一道论文题 后缀数组已弃疗,强行需要DC3构造,懒得(不会)写 ..]of longint; n,m,i,j,len,ans,st:longint; ch:ansistring; flag:boolean; function min(x,y:longint):longint; begin if x<y then exit(x); exit(y); end; function cmp(a,b,l:longint):boolean; begin…
[SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<set…
[CF913G]Power Substring 题意:T组询问,每次给定一个数a,让你求一个k,满足$2^k$的10进制的后$min(100,length(k))$位包含a作为它的子串.你只需要输出一个k,不需要最小化k的值,保证有解. $T\le 2000,a\le 10^{11}$ 题解:神题. 假设a有n位,$2^k=x$,$x=a\times 10^m+b(\mod 10^{n+m})$,我们显然有$k\ge n+m$,所以$2^{n+m}\mid x$,又因为$2^{n+m}\mid…
[题目链接] 点击打开链接 [算法] KMP 如果字符串中存在循环节,则next[len] = (循环节个数 - 1) * 循环节长度 循环节个数 = len / (len - next[len]) [代码] #include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #in…
Power Strings 题意 给出一个字符串s,求s最多由几个相同的字符串重复而成(最小循环节的重复次数) 思路 之前学习KMP的时候做过. 我的思路是:枚举字符串的长度,对于当前长度k,判断\(lcp(1,k+1)>=k\),\(lcp(k+1,2k+1)>=k\),\(lcp(3k+1,4k+1)>=k\)....是否都成立 但这样复杂度有点高,就找了一下题解. 其实只需要判断\(lcp(1,k+1)\)是否是\(n-k\)就可以了. 具体原理如下图: 参考博客 对于\(lcp(…
[哈希和哈希表]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.…
[题目大意] 已知多项式方程: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.…
图片来源: https://blog.csdn.net/dylan_frank/article/details/78177368 [题意]: 对于每一个节点来说有多少对相同的子树. [题解]: 利用层数进行hash,返回到对应的节点,最后标记后用等差数列来求出所有方案数. #pragma GCC optimize(2) #include<bits/stdc++.h> using namespace std; typedef unsigned long long ull ; ; ; vector…
[题目链接] # 10038. 「一本通 2.1 练习 4」A Horrible Poem [参考博客] A Horrible Poem (字符串hash+数论) [题目描述] 给出一个由小写英文字母组成的字符串 SS,再给出 qq 个询问,要求回答 SS 某个子串的最短循环节.如果字符串 BB 是字符串 AA 的循环节,那么 AA 可以由 BB 重复若干次得到. [算法]-首先对于长度为 lenlen 的子串,循环节长度为 xx 的充要条件:[1,len−x][1,len−x]串的哈希值等于 …
[来源]:bzoj3916 [参考博客] BZOJ3916: [Baltic2014]friends [ 哈希和哈希表]Three Friends [Baltic2014][BZOJ3916]friends [题解] 首先hash整个串,然后分成三种情况,分别是前半段,中间,后半段,三段的字母试图去掉看能否拼起来. 如果可以,那么还需要考虑是否为唯一的. 唯一的意思是: 串S,如果有选择两个不同的串S也能构成这个原串. 代码还是参考别人写的. #include<cstdio> #include…
题目描述: 解决思路: 此题较简单,和前面[LeetCode67]方法一样. Java代码: public class LeetCode415 { public static void main(String[] args) { String a="1",b="9"; System.out.println(a+"和"+b+"相加的结果是:"+new Solution().addStrings(a, b)); } } class…
[链接]:A [题意]:给你n个数的序列和k.判断是否可以三个数组成k(同一个数可以拿多次) [分析]:每个数vis记录一下.2层循环.两数之和不超过k以及剩下的数出现在序列中那么ok. [代码]: #include <bits/stdc++.h> #define ll long long #define pb push_back #define inf 0x3f3f3f3f #define pll pair<ll,ll> #define rep(i,a,b) for(int i=…
[来源] https://loj.ac/problem/2427 [参考博客] LOJ#2427. 「POI2010」珍珠项链 Beads [题解]: 复杂度计算: 暴力枚举k每次计算是n/2+n/3+n/4+...+1的,用调和级数算是…
描述 Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is de…
Time Limit: 3000MSMemory Limit: 65536K Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication…
看的<后缀数组——处理字符串的有力工具>这篇论文,在那里这道题是用后缀数组实现的,复杂度为$O(nlogn)$,很明显长度为$2×10^6$的数据会TLE,所以必需得用复杂度为$O(n)$的KMP算法.第一次写KMP,我好弱啊QAQ KMP: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 2000003; char s[N];…
终于靠着理解写出KMP了,两种KMP要代码中这种才能求循环节.i-next[i]就是循环节. #include<cstdio> #define N 1000005 char s[N]; int next[N]; void solve(){ int ans; int k=-1,i=0; next[i]=k; while(s[i]){ if(k==-1||s[i]==s[k]){//记住两个都是等于 i++; k++; next[i]=k; }else k=next[k]; } ans=i-nex…
题目描述 如果一个字符串可以被拆分为AABBAABB的形式,其中 A和 B是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串aabaabaaaabaabaa,如果令 A=aabA=aab,B=aB=a,我们就找到了这个字符串拆分成 AABBAABB的一种方式. 一个字符串可能没有优秀的拆分,也可能存在不止一种优秀的拆分.比如我们令 A=aA=a,B=baaB=baa,也可以用 AABBAABB表示出上述字符串:但是,字符串 abaabaaabaabaa 就没有优秀的拆分.…
题意: 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…
题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of charac…
  I know many people are bored of the string splitting problem, but it still seems to come up almost daily on forum and Q & A sites like StackOverflow. This is the problem where people want to pass in a string like this: EXEC dbo.UpdateProfile @UserI…
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters.…
题目链接[https://www.oj.swust.edu.cn/problem/show/2610] 题意:给你一个字符串,让你判断这个字符串是不是回文串,字符串的长度是1<len<1e7,内存是4096KB. 题解:首先这1e7个字符是存不下的,1e71024=9765KB>4096kB.那么怎么办?字符串哈希,先对字符串的前半部分进行哈希,然后在对字符串后半部分进行哈希,如果两部分的哈希值相同,那么这个字符串就是回文串. BKDRH哈希,哈希公式为has=has*seed+s[i]…
Codeforces 25E Test E. Test Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings - input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives…
Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45515   Accepted: 18181 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quot…