String Problem

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
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   3371  3372  3373  3375  3376 

题意:输出字典序最小最大开始的序列编号以及出现过几次;

这里重点说求最大最小字典序的方法;(以下摘自点击打开链接)

(1)  利用两个指针p1, p2。初始化时p1指向s[0], p2指向s[1]。

(2)  k = 0开始,检验s[p1+k] 与 s[p2+k] 对应的字符是否相等,如果相等则k++,一直下去,直到找到第一个不同,(若k试了一个字符串的长度也没找到不同,则那个位置就是最小表示位置,算法终止并返回)。则该过程中,s[p1+k] 与 s[p2+k]的大小关系,有三种情况:

     (A). s[p1+k] > s[p2+k],则p1滑动到p1+k+1处 --- 即s1[p1->p1+k]不会 

      是该循环字符串的“最小表示”的前缀。 k置为0

     (B). s[p1+k] < s[p2+k],则p2滑动到p2+k+1处, k置为0

     (C). s[p1+k] = s[p2+k],则 k++; if (k == len) 返回结果。

    注:这里滑动方式有个小细节,若滑动后p1 == p2,将正在变化的那个指针再+1。直到p1、p2把整个字符串都检验完毕,返回两者中小于 len 的值。

(3)   如果 k == len, 则返回p1与p2中的最小值


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
const int MAXN=1000000+10;
//const int MAXM=100+10;
//int x[MAXN];
char t[MAXN];
int nextra[MAXN];
int m;
void get_next()
{
    int i,k;
    i=0,k=-1,nextra[0]=-1;
    while(i<m)
    {
        if(k==-1||t[i]==t[k])
        {
            i++;
            k++;
            nextra[i]=k;
        }
        else
            k=nextra[k];
    }
}
int judge(int flag)
{
    int p1=0,p2=1,k=0;
    while(p1<m&&p2<m&&k<m)
    {
        int tt=(t[(p1+k)%m]-t[(p2+k)%m]);
        if(tt==0) k++;
        else
        {
            if(!flag)
            {
                if(tt<0) p2+=k+1;
                else p1+=k+1;
            }
            else
            {
                if(tt<0) p1+=k+1;
                else p2+=k+1;
            }
            if(p1==p2) p2++;
            k=0;
        }
    }
    return min(p1,p2);
}
int main()
{
    while(~scanf("%s",t))
    {
        m=strlen(t);
        int minn=judge(0);
        int maxx=judge(1);
        get_next();
        int k=m-nextra[m],ans;
        if(m%k==0) ans=m/k;
        else ans=1;
        printf("%d %d %d %d\n",minn+1,ans,maxx+1,ans);
    }
    return 0;
}




写个小东西:c++中的substr函数;

#include<string>
#include<iostream>
using namespace std;
int main()
{
    string s="abcdefg";
    int len=s.size();
    string s1=s.substr(3);//返回从3开始到最后的子串
    string s2=s.substr(0,len-2);//返回从0开始,长度为len-1的子串
    cout<<s1<<endl;
    cout<<s2<<endl;
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值