UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>
L - Ferris Wheel String
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 43000/43000KB (Java/Others)

Have you ever been to London?
Our Master Qiu will tell you how amazing in London and how funny a Ferris Wheel String is.
One day, while our Master Qiu was recovering Great Britain, he was thinking about an interesting problem about string. In front of him, a London Ferris Wheel was revolving. There were some seats on the large Ferris Wheel. However, in Master Qiu's mind, each seat was a lower-case letter so that the Ferris Wheel was a beautiful string that was revolving all the time. Let's call it Ferris Wheel String.
In the revolving operation, you can put several front letters to the last of the string. Of course, you can also put all the letters to the last, so you will get the string itself. For example, you can change string abcd into 4 strings, such as bcda, cdab, dabc, abcd. Obviously, in the revolving operation, a string of length n can be changed into n strings.
Master Qiu found out an interesting phenomenon that the Ferris Wheel String spent exactly one second revolving one letter. For example, at the beginning(0 second),the Ferris Wheel String was abcd,after one second,it became bcda ... and after four seconds, it became abcd.
Master Qiu is a Romantic guy. He thought after exactly K second(s), the Ferris Wheel String became extremely beautiful.
Now, he would like to ask you a question:
After exactly K second(s), among the n strings obtained by revolving a string of length n, how many distinct strings are lexicographically smaller than the beautiful string, how many distinctstrings are lexicographically equal to the beautiful string and how many distinct strings are lexicographically bigger than the beautiful string ?
(If you really can not understand Master Qiu's mind, see examples and Hint)
Input
The first line contains a string of length n(1≤n≤2⋅105) and the second line contains an integer k(1≤k≤n).
The string only contains lower-case letters.
Output
Output three integers separated by two spaces that are your answer to Master Qiu. Do not output anything after the last integer.
Sample input and output
| Sample Input | Sample Output |
|---|---|
abcabc |
1 1 1 |
aaaaaaa |
0 1 0 |
himverihimverihimveri |
0 1 6 |
lvhqsjtdgckrznjsbargcojiyuf |
7 1 19 |
abbabaabbaababbabaababbaabbaba |
3 1 26 |
Hint
Let's define that < means lexicographically smaller,> means lexicographically bigger and = means lexicographically equal.
Also, ans1 means the number of strings < bcabca,ans2 means the number of strings = bcabca and ans3 means the number of strings > bacabca.
Explain for lexicography :
Let's only consider lexicographical order between two strings when |S|=|T|.
If S<T, there exits a position p (0≤p<|S|) satisfying that Si=Ti when 0≤i<p but Sp<Tp.
If S>T, there exits a position p (0≤p<|S|) satisfying that Si=Ti when 0≤i<p but Sp>Tp.
If S=T, then Si=Ti for all i (0≤i<|S|)
And we all know that a<b<c< ⋯ <x<y<z.
Explain for the first example :
The Ferris Wheel String is abcabc and K=1 , so that the beautiful string is bcabca
After 1 second, it becomes cabcab, cabcab>bcabca;ans3+=1;
After 2 seconds, it becomes abcabc, abcabc<bcabca;ans1+=1;
After 3 seconds, it becomes bcabca, bcabca=bcabca;ans2+=1;
After 4 seconds, it becomes cabcab. But it has appeared. So, do not count it.
After 5 seconds, it becomes abcabc. But it has appeared. So, do not count it.
After 6 seconds, it becomes bcabca. But it has appeared. So, do not count it.
Therefore, ans1=1, ans2=1 and ans3=1.
Use double hash rather than single hash if you wanna hash.
解题报告:
首先我们将移动后的字符串 * 2处理出来,之后Hash处理,注意使用双哈希.
相同字符换的判断我们将字符串本身的哈希值再哈希即可..
那么字典序如何判断呢?
我们考虑字典序时,前面一段都是一样的,之后在某个位置就不同了,满足二分性质,因此我们通过二分来找到第一个不一样的位置,从而比较字典序的大小
#include <iostream>
#include <cstring>
#include <cstdio>
typedef long long ll;
using namespace std;
const int maxn = 2e5 + , p1 = , mod1 = 1e9 + , p2 = , mod2 = 1e9 + , MaxHashSize = , MaxStatusSize = 2e5 + ;
char str[maxn*],temp[maxn];
int k,len,stdhash1,stdhash2,head[MaxHashSize],next_new[MaxStatusSize],size = ;
ll hash1[maxn*],fac1[maxn+],hash2[maxn*],fac2[maxn+];
typedef struct status
{
int hash1,hash2;
}; status st[MaxStatusSize]; int gethashvalue(int l,int r,int id)
{
ll ans;
if (id == )
{
ans = (hash1[r] - hash1[l-]*fac1[r-l+])%mod1;
if (ans < )
ans += mod1;
return ans;
}
else
{
ans = (hash2[r] - hash2[l-]*fac2[r-l+])%mod2;
if (ans < )
ans += mod2;
return ans;
}
} void init_hash()
{
memset(head,-,sizeof(head));
hash1[] = ,hash2[] = ;
for(int i = ; i <= *len ; ++ i )
hash1[i] = (hash1[i-]*p1 + str[i]) % mod1;
for(int i = ; i <= *len ; ++ i )
hash2[i] = (hash2[i-]*p2 + str[i]) % mod2;
fac1[] = ;
for(int i = ; i <= maxn ; ++ i)
fac1[i] = (fac1[i-]*p1) % mod1;
fac2[] = ;
for(int i = ; i <= maxn ; ++ i)
fac2[i] = (fac2[i-]*p2) % mod2;
} int HashValue(const status &x)
{
return (x.hash1 + x.hash2) % MaxHashSize;
} bool insert(int id)
{
int val = HashValue(st[id]);
int u = head[val];
while(u != -)
{
if (!memcmp(&st[u],&st[id],sizeof(status)))
return false;
u = next_new[u];
}
next_new[id] = head[val];
head[val] = id;
return true;
} void dump(int l,int r)
{
for(int i = l ; i <= r ; ++ i)
printf("%c",str[i]);
printf("\n");
} int main(int argc,char *argv[])
{
scanf("%s%d",str+,&k);len = strlen(str+);
memcpy(temp,str+,k);memcpy(str+,str+k+,len-k);memcpy(str+len-k+,temp,k);memcpy(str+len+,str+,len);init_hash();
stdhash1 = gethashvalue(,len,);
stdhash2 = gethashvalue(,len,);
ll ans1 = , ans2 = , ans3 = ; // < = >
for(int i = ; i <= len+ ; ++ i)
{
st[size].hash1 = gethashvalue(i,i+len-,);
st[size].hash2 = gethashvalue(i,i+len-,);
if (!insert(size))
{
//dump(i,i+len-1);
continue;
}
int thishash1 = st[size].hash1;
int thishash2 = st[size++].hash2;
if (thishash1 == stdhash1 && thishash2 == stdhash2)
{
ans2 ++ ;
continue;
}
if (str[i] != str[])
{
if (str[i] < str[])
ans1++;
else
ans3++;
continue;
}
int l = , r = len-;
while(l < r)
{
int mid = l + (r-l+)/;
int stdh1 = gethashvalue(,mid,);
int stdh2 = gethashvalue(,mid,);
int thish1 = gethashvalue(i,i+mid-,);
int thish2 = gethashvalue(i,i+mid-,);
if (stdh1 == thish1 && stdh2 == thish2)
l = mid;
else
r = mid - ;
}
if (str[i + l] < str[ + l])
ans1++;
else
ans3++;
}
printf("%lld %lld %lld\n",ans1,ans2,ans3);
return ;
}
UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>的更多相关文章
- UESTC_Palindromic String 2015 UESTC Training for Search Algorithm & String<Problem M>
M - Palindromic String Time Limit: 3000/1000MS (Java/Others) Memory Limit: 128000/128000KB (Java ...
- UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm & String<Problem N>
N - 韩爷的梦 Time Limit: 200/100MS (Java/Others) Memory Limit: 1300/1300KB (Java/Others) Submit Stat ...
- 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】
O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...
- 2015 UESTC Training for Search Algorithm & String - J - 全都是秋实大哥 【KMP】
给出一个字符串,求每个前缀的最小循环节长度,并输出整个字符串的最小循环节.字符串长度为3*10^6 找循环节这种问题还是要用KMP对于长度为i的字符串 i%(i-f[i])==0 此时,它的最小循环节 ...
- UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm & String<Problem K>
K - 秋实大哥の恋爱物语 Time Limit: 5000/2000MS (Java/Others) Memory Limit: 32000/32000KB (Java/Others) Su ...
- UESTC_全都是秋实大哥 2015 UESTC Training for Search Algorithm & String<Problem J>
J - 全都是秋实大哥 Time Limit: 5000/2000MS (Java/Others) Memory Limit: 32000/32000KB (Java/Others) Subm ...
- UESTC_Infected Land 2015 UESTC Training for Search Algorithm & String<Problem G>
G - Infected Land Time Limit: 6000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others ...
- UESTC_Eight Puzzle 2015 UESTC Training for Search Algorithm & String<Problem F>
F - Eight Puzzle Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) ...
- UESTC_吴队长征婚 2015 UESTC Training for Search Algorithm & String<Problem E>
E - 吴队长征婚 Time Limit: 10000/4000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
随机推荐
- cf446A DZY Loves Sequences
A. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 福建省队集训被虐记——DAY2
唉--第二天依然被虐--但是比第一天好一点--我必须负责任的指出:志灿大神出的题比柯黑的不知道靠谱到哪里去了--柯黑出的简直不可做 但是被虐的命运是无法改变的--求各位神犇别D我 黄巨大真是强啊,不愧 ...
- Web UI 网站用户界面设计命名规范
Web UI 网站用户界面设计命名规范 WEB UI设计命名规范,也就是网站用户界面设计(网页设计)命名规范. 这套规范并非单纯的CSS.html或JavaScript命名规范,它涉及了很多使用Pho ...
- python海明距离 - 5IVI4I_I_60Y的日志 - 网易博客
python海明距离 - 5IVI4I_I_60Y的日志 - 网易博客 python海明距离 2009-10-01 09:50:41| 分类: Python | 标签: |举报 |字号大中小 ...
- FFmpeg缩放swscale详解 <转>
利用ffmpeg进行图像数据格式的转换以及图片的缩放应用中,主要用到了swscale.h文件中的三个函数,分别是: struct SwsContext *sws_getContext(int srcW ...
- hdu 5656 CA Loves GCD(dp)
题目的意思就是: n个数,求n个数所有子集的最大公约数之和. 第一种方法: 枚举子集,求每一种子集的gcd之和,n=1000,复杂度O(2^n). 谁去用? 所以只能优化! 题目中有很重要的一句话! ...
- js实现a标签超链接提交form表单的方法
<a class="regButton" id="saveRegister" onclick="document.getElementBy ...
- Traceroute原理介绍
一.路由追踪 路由跟踪,就是获取从主机A到达目标主机B这个过程中所有需要经过的路由设备的转发接口IP. 二.ICMP协议 Internet控制报文协议(internet control message ...
- VS2008 由于应用程序配置不正确,应用程序未能启动。重新安装应用程序可能会纠正这个问题。
提示这个错误,自己的程序是在VS2008下编译的C/C++ win32程序,自己当时在win7上开发测试,都没有问题,正常使用,也在另一台xp系统上也试了,都没有问题.就发给客户了,没想到有些客户竟然 ...
- mmc加工配套问题
题目如下,本题还有其它解.