HDU 3374 String Proble

String Problem

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

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

求循环节,并求字典序最大最小出现的次数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
char a[1000010];
int p[1000010];
void getnext()
{
  int i=0,j=-1;
  p[0]=-1;
  while(a[i]!=‘\0‘)
  {
    if(j==-1 || a[i]==a[j])
      p[++i]=++j;
    else j=p[j];
  }
}
int check(bool flag)
{
  int i=0,j=1,k=0;
  while(i<strlen(a) && j<strlen(a) && k<strlen(a))
  {
    int t=a[(j+k)%strlen(a)]-a[(i+k)%strlen(a)];
    if(t==0) k++;
    else
    {
      if(flag)
      {
        if(t>0) j+=k+1;
        else i+=k+1;
      }
      else
      {
        if(t<0) j+=k+1;
        else i+=k+1;
      }
      if(i==j) j++;
      k=0;
    }
  }
  return min(i,j);
}
int main()
{
  while(scanf("%s",a)!=EOF)
  {
    int minn=check(true);
    int maxn=check(false);
    getnext();
    int ans=(strlen(a)%(strlen(a)-p[strlen(a)]))==0?strlen(a)/(strlen(a)-p[strlen(a)]):1;
    printf("%d %d %d %d\n",minn+1,ans,maxn+1,ans);
  }
  return 0;
}
时间: 2024-08-10 16:06:19

HDU 3374 String Proble的相关文章

hdu 3374 String Problem (kmp+最大最小表示法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:输出最大和最小的是从哪一位开始的,同时输出最小循环节的个数. 这里简单介绍对字符串最小表示的方法: (1)  利用两个指针p1, p2.初始化时p1指向s[0], p2指向s[1]. (2)  k = 0开始,检验s[p1+k] 与 s[p2+k] 对应的字符是否相等,如果相等则k++,一直下去,直到找到第一个不同,(若k试了一个字符串的长度也没找到不同,则那个位置就是最小表示位置,

HDU 3374 String Problem (KMP+最大最小表示)

KMP,在有循环节的前提下: 循环节 t = len-next[len], 个数num = len/(len-next[len]); 个人理解,如果有循环节,循环节长度必定小于等于len/2, 换句话说next[len]>=len/2; 对于len%(len-next)!=0的这种情况不讨论,循环节不存在. 下面是假设循环节存在的情况 当次数等于2, 对于abcabc这种情况就不用说了,len = 6, next[len] = 3; 当次数大于2, 对于串a1 a2 a3 a4 a5 a6 a7

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 3374 String Problem (KMP+最大最小表示)

转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526       by---cxlove 题目:输出最大最小表示是从哪一位开始,而且输出数量 http://acm.hdu.edu.cn/showproblem.php?pid=3374 数量好求,肯定是字符串的循环节,循环节可以直接通过KMP的Next数组得到.这个和yobobobo曾经研究过半天.. 对于最大最小表示法,就是将字符串不断旋转,得到字典序最大或者最小的.

HDU 3374 String 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 a

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.

hdu 6194 string string string(后缀数组)

题目链接:hdu 6194 string string string 题意: 给你一个字符串,给你一个k,问你有多少个子串恰好在原串中出现k次. 题解: 后缀数组求出sa后,用height数组的信息去找答案. 每次用k长度的区间去卡height数组,求出该区间的lcp. 该区间的贡献就是ans=lcp-max(height[i],height[i+k]). 如果ans<=0,就不贡献. 比如 2 aaa 后缀数组为: 1 a 2 aa 3 aaa height为 0,1,2 现在扫到[1,2],

hdu 4821 String(字符串hash)

题目链接:hdu 4821 String 题意: 给你一个字符串,问你有多少子串,满足长度为m*len,并且这个子串能分成m个len长度的不同串. 题解: BKDRhash+map来判重.注意的是要以len长分类来扫,这样才不会超时. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 typedef unsigned long long ull; 5 co

HDU 2476 String painter(字符串转变)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 题意:给定两个长度相同的串A和B.每次操作可以将A的连续一段改变为另一个字母.求将A转换成B最少需要多少次操作? 思路:首先,我们假设没有A串,那么这就跟 BZOJ1260是一样的了,即答案为DFS(0,n-1)...但是这里有了A串就有可能使得操作次数更少.因为可能有些对应位置字母是相同的.我们设 ans[i]表示前i个字母变成一样的,那么若A[i]=B[i]则ans[i]=ans[i-1]