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)

Submit Status

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 bcdacdabdabcabcd. 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 1
aaaaaaa
3
0 1 0
himverihimverihimveri
18
0 1 6
lvhqsjtdgckrznjsbargcojiyuf
19
7 1 19
abbabaabbaababbabaababbaabbaba
29
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 + 500 , p1 = 2807303 , mod1 = 1e9 + 7 , p2 = 5614657 , mod2 = 1e9 + 9, MaxHashSize = 1403641 , MaxStatusSize = 2e5 + 500;
char str[maxn*2],temp[maxn];
int k,len,stdhash1,stdhash2,head[MaxHashSize],next_new[MaxStatusSize],size = 0;
ll hash1[maxn*2],fac1[maxn+50],hash2[maxn*2],fac2[maxn+50];
typedef struct status
{
 int hash1,hash2;
};

status st[MaxStatusSize];

int gethashvalue(int l,int r,int id)
{
   ll ans;
   if (id == 1)
    {
       ans = (hash1[r] - hash1[l-1]*fac1[r-l+1])%mod1;
       if (ans < 0)
        ans += mod1;
       return ans;
    }
   else
    {
       ans = (hash2[r] - hash2[l-1]*fac2[r-l+1])%mod2;
       if (ans < 0)
        ans += mod2;
       return ans;
    }
}

void init_hash()
{
   memset(head,-1,sizeof(head));
   hash1[0] = 0,hash2[0] = 0;
   for(int i = 1 ; i <= 2*len ; ++ i )
    hash1[i] = (hash1[i-1]*p1 + str[i]) % mod1;
   for(int i = 1 ; i <= 2*len ; ++ i )
    hash2[i] = (hash2[i-1]*p2 + str[i]) % mod2;
   fac1[0] = 1;
   for(int i = 1 ; i <= maxn ; ++ i)
    fac1[i] = (fac1[i-1]*p1) % mod1;
   fac2[0] = 1;
   for(int i = 1 ; i <= maxn ; ++ i)
    fac2[i] = (fac2[i-1]*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 != -1)
   {
         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+1,&k);len = strlen(str+1);
  memcpy(temp,str+1,k);memcpy(str+1,str+k+1,len-k);memcpy(str+len-k+1,temp,k);memcpy(str+len+1,str+1,len);init_hash();
  stdhash1 = gethashvalue(1,len,1);
  stdhash2 = gethashvalue(1,len,2);
  ll ans1 = 0 , ans2 = 0 , ans3 = 0 ; // <  =   >
  for(int i = 2 ; i <= len+1 ; ++ i)
   {
         st[size].hash1 = gethashvalue(i,i+len-1,1);
         st[size].hash2 = gethashvalue(i,i+len-1,2);
         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[1])
        {
              if (str[i] < str[1])
               ans1++;
              else
               ans3++;
              continue;
       }
      int l = 1 , r = len-1;
      while(l < r)
       {
             int mid = l + (r-l+1)/2;
             int stdh1 = gethashvalue(1,mid,1);
             int stdh2 = gethashvalue(1,mid,2);
             int thish1 = gethashvalue(i,i+mid-1,1);
             int thish2 = gethashvalue(i,i+mid-1,2);
             if (stdh1 == thish1 && stdh2 == thish2)
              l = mid;
             else
              r = mid - 1;
       }
      if (str[i + l] < str[1 + l])
       ans1++;
      else
       ans3++;
   }
  printf("%lld %lld %lld\n",ans1,ans2,ans3);
  return 0;
}
时间: 2024-10-12 18:17:05

UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>的相关文章

UESTC_Palindromic String 2015 UESTC Training for Search Algorithm &amp; String&lt;Problem M&gt;

M - Palindromic String Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 128000/128000KB (Java/Others) Submit Status 秋实大哥喜欢探索新鲜事物,最近他发明了一种新型回文串,叫K重回文串!今天他想用它来考考小朋友们. 秋实大哥给出了与K重回文串有关的信息 任何字符串都属于0重回文串,包括空字符串. 一个长度为N的字符串S,S是K(k≥1)重回文串,当且仅当S是回文串,且其

UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm &amp; String&lt;Problem N&gt;

N - 韩爷的梦 Time Limit: 200/100MS (Java/Others)     Memory Limit: 1300/1300KB (Java/Others) Submit Status 一天,韩爷去百度面试,面试官给了他这么一个问题. 给你2万个字符串,每个字符串长度都是100,然后把2万个字符串丢入一个 set< string >g 中,问最终set里含有多少个元素? g 是一个用来存储字符串.具有去重功能的容器,即相同字符串在 g 中只能保留一个. 两个字符串相等,当且

UESTC_全都是秋实大哥 2015 UESTC Training for Search Algorithm &amp; String&lt;Problem J&gt;

J - 全都是秋实大哥 Time Limit: 5000/2000MS (Java/Others)     Memory Limit: 32000/32000KB (Java/Others) Submit Status 秋实大哥是一个多愁善感的人,偶尔也会唱唱两句伤情的歌.每次唱完后,秋实大哥都能解决一道问题!这次也不例外. 秋实大哥告诉了你 一些关于这个问题的信息 如果一个字符串S是由若干个子串a连续拼接而成的,则称a是S的循环节,即A=a+a+...+a.比如 aba 是 abaabaaba

UESTC_Infected Land 2015 UESTC Training for Search Algorithm &amp; String&lt;Problem G&gt;

G - Infected Land Time Limit: 6000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status The earth is under an attack of a deadly virus. Luckily, prompt actions of the Ministry of Health against this emergency successfully

UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm &amp; String&lt;Problem K&gt;

K - 秋实大哥の恋爱物语 Time Limit: 5000/2000MS (Java/Others)     Memory Limit: 32000/32000KB (Java/Others) Submit Status 传说有这么一个故事! 在一个月白风清的晚上,秋实大哥约一位他心仪的妹子一起逛校园,浪漫的秋实大哥决定在当晚对妹子表白.“XXXXX...”,秋实大哥温情地说完了准备已久的话.而妹子决定用一种浪漫的方式接受秋实大哥(其实妹子早已对秋实大哥动心,这一刻她早已迫不及待了,但还是决定

UESTC_Eight Puzzle 2015 UESTC Training for Search Algorithm &amp; String&lt;Problem F&gt;

F - Eight Puzzle Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sli

UESTC_吴队长征婚 2015 UESTC Training for Search Algorithm &amp; String&lt;Problem E&gt;

E - 吴队长征婚 Time Limit: 10000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 吴队长征婚这件事因为请客而没有传出去(虽然他忘了请一个队吃饭),于是吴队长高兴地玩起了木棒.吴队长拿了一些长度相同的木(guang)棒(gun),随机的把它们截成了N段,每一段最长50.现在他想把这些木棒还原成原来的状态,但是他忘记了原来的木棒有多少根,也忘记了每一根有多长.请帮助

UESTC_基爷的中位数 2015 UESTC Training for Search Algorithm &amp; String&lt;Problem D&gt;

D - 基爷的中位数 Time Limit: 5000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 给你N个数,X1,X2,...,XN, 基爷让我们计算任意两个数差的绝对值 ∣Xi−Xj∣ (1≤i<j≤N) . 这样,我们可以得到 C2N 个数. 现在,基爷希望聪明的你能用一个简单的程序求出这 C2N 个数的中位数! Input 输入有多组数据. 每组数据,第一行一个整数 N

UESTC_基爷与加法等式 2015 UESTC Training for Search Algorithm &amp; String&lt;Problem C&gt;

C - 基爷与加法等式 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 一天,上小学的妹妹跑过来问基爷一道字母加法等式,基爷不假思索的便给出了一组可行解. 聪明的你发现,一个字母等式可能有多种不同解,于是你想编个程序计算一下 Input 输入包含多组数据. 每组数据第一行一个整数n,表示有n个字符串 3 ≤ n ≤ 10 接下来n行,每行有1个最多只