String Problem

String Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2000    Accepted Submission(s): 875

Problem Description

Give you a string with length N, you can generate N strings by left shifts. For example let consider the string “SKYLONG”, we can generate seven strings:
String Rank 
SKYLONG 1
KYLONGS 2
YLONGSK 3
LONGSKY 4
ONGSKYL 5
NGSKYLO 6
GSKYLON 7
and lexicographically first of them is GSKYLON, lexicographically last is YLONGSK, both of them appear only once.
  Your task is easy, calculate the lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), its times, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.

Input

Each line contains one line the string S with length N (N <= 1000000) formed by lower case letters.

Output

Output four integers separated by one space, lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), the string’s times in the N generated strings, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.

Sample Input

abcder

aaaaaa

ababab

Sample Output

1 1 6 1

1 6 1 6

1 3 2 3

Author

WhereIsHeroFrom

Source

HDOJ Monthly Contest – 2010.04.04

题意:给你一字符串,问最小字典序还有最大字典序的最小下标,以及包含最小字典序和最大字典序的串的开始的不同下标个数。

不知道动不动就TLE什么鬼……

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<algorithm>
  5
  6 using namespace std;
  7
  8 const int maxn = 1e6+7;
  9
 10 char s[maxn*2], str[maxn];
 11 int Next[maxn], len;
 12
 13 void Getnext(char s[])
 14 {
 15     int j, k;
 16     j = 0;
 17     k = Next[0] = -1;
 18     while(j < len)
 19     {
 20         while(k != -1 && s[j] != s[k])
 21             k = Next[k];
 22         Next[++j] = ++k;
 23     }
 24 }
 25
 26 int Getmax(char s[])   // 最大最小表示法
 27 {
 28     int i, j;
 29     i = 0;
 30     j = 1;
 31     while(i < len && j < len)
 32     {
 33         int k = 0;
 34         while(s[i+k] == s[j+k] && k < len)
 35             k++;
 36         if(k == len)
 37             break;
 38
 39         if(s[i+k] > s[j+k])
 40         {
 41             if(j+k > i)
 42                 j = j+k+1;
 43             else
 44                 j = i + 1;
 45         }
 46         else
 47         {
 48             if(i+k > j)
 49                 i = i + k + 1;
 50             else
 51                 i = j + 1;
 52         }
 53     }
 54     return min(i, j);
 55 }
 56
 57 int Getmin(char s[])
 58 {
 59     int i, j;
 60     i = 0;
 61     j = 1;
 62     while(i < len && j < len)
 63     {
 64         int k = 0;
 65         while(s[i+k] == s[j+k] && k < len)
 66             k++;
 67         if(k == len)
 68             break;
 69         if(s[i+k] < s[j+k])
 70         {
 71             if(j+k > i)
 72                 j = j+k+1;
 73             else
 74                 j = i + 1;
 75         }
 76         else
 77         {
 78             if(i+k > j)
 79                 i = i + k + 1;
 80             else
 81                 i = j + 1;
 82         }
 83     }
 84     return min(i, j);
 85 }
 86
 87 int main()
 88 {
 89     while(gets(str))
 90     {
 91         len = strlen(str);
 92         strcpy(s, str);
 93         strcat(s, str);
 94
 95         memset(Next, 0, sizeof(Next));
 96
 97         Getnext(str);
 98         int t = 1, k = len - Next[len];   // k 最小循环节的长度,最小循环节,神奇的东西……
 99
100         if(len % k == 0)
101             t = len / k;
102         printf("%d %d %d %d\n", Getmin(s)+1, t, Getmax(s)+1, t);
103     }
104     return 0;
105 }

写代码你就好好写代码………………………………………………………………………………………………

时间: 2024-10-10 17:01:55

String Problem的相关文章

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

HDU 5008 Boring String Problem(西安网络赛B题)

HDU 5008 Boring String Problem 题目链接 思路:构造后缀数组,利用height的数组能预处理出每个字典序开始的前缀和有多少个(其实就是为了去除重复串),然后每次二分查找相应位置,然后在往前往后找一下sa[i]最小的 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int

HDOJ3374 String Problem [KMP最小循环节点]+[最小(大)表示法]

String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1442    Accepted Submission(s): 645 Problem Description Give you a string with length N, you can generate N strings by left shifts

String Problem hdu 3374 最小表示法加KMP的next数组

String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1492    Accepted Submission(s): 662 Problem Description Give you a string with length N, you can generate N strings by left shifts.

HDOJ 5008 Boring String Problem

后缀数组+RMQ+二分 后缀数组二分确定第K不同子串的位置 , 二分LCP确定可选的区间范围 , RMQ求范围内最小的sa Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 661    Accepted Submission(s): 183 Problem Description In thi

HDOJ3374 String Problem 【KMP】+【最小表示法】

String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1512    Accepted Submission(s): 668 Problem Description Give you a string with length N, you can generate N strings by left shifts

bestcoder 47# wyh2000 and a string problem (水题)

wyh2000 and a string problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 484    Accepted Submission(s): 232 Problem Description Young theoretical computer scientist wyh2000 is teaching you

hdu 3374 String Problem

String Problem http://acm.hdu.edu.cn/showproblem.php?pid=3374 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3153    Accepted Submission(s): 1272 Problem Description Give you a string with leng

hdu 5008 Boring String Problem(后缀数组)

题目链接:hdu 5008 Boring String Problem 题目大意:给定一个字符串,初始状态l,r为0,每次询问子串中字典序第l^r^v+1的子串区间,对于重复的输出下标小的. 解题思路:后缀数组,对给定字符串做后缀数组,然后根据height数组确定每个位置做为起点的子串有多少,然后二分查找确定起点位置,但是因为子串的重复的要输出下表小的,所以确定起点后还要确定字典序最小的下标. #include <cstdio> #include <cstring> #includ