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


    String Problem

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

    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

    题目链接:HDU 3374

    求最小与最大字典序的所需左移次数和出现次数,左移次数用两倍的s来匹配,在计数的时候把第一次匹配成功的开头位置记录下来即可,计数过程中i要小于len*2-1,比如abcde拿来当主串扩展两倍后变成abcdeabcde,其实中间一共左移所生成的串只有len-1个即到edbde就结束了,因此不能再去匹配最后一个e,不然像abcde本身拿去匹配这个串就出现了两次了……,代码写起来比较麻烦,顺便复习一下好久没敲了的KMP搜索函数。

    代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdlib>
    #include<sstream>
    #include<cstring>
    #include<bitset>
    #include<cstdio>
    #include<string>
    #include<deque>
    #include<stack>
    #include<cmath>
    #include<queue>
    #include<set>
    #include<map>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define CLR(x,y) memset(x,y,sizeof(x))
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    typedef pair<int,int> pii;
    typedef long long LL;
    const double PI=acos(-1.0);
    const int N=1000010;
    char s[N],fir[N],las[N],temp[N<<1];
    int nxta[N],nxtb[N];
    int len,L;
    void init()
    {
    	CLR(s,0);
    	CLR(fir,0);
    	CLR(las,0);
    	CLR(nxta,0);
    	CLR(nxtb,0);
    	CLR(temp,0);
    }
    void getnext(char s[],int nxt[])
    {
    	int j=0,k=nxt[0]=-1;
    	len=strlen(s);
    	while (j<len)
    	{
    		if(k==-1||s[j]==s[k])
    			nxt[++j]=++k;
    		else
    			k=nxt[k];
    	}
    }
    int minp()
    {
    	int i=0,j=1,k=0;
    	while (i<len&&j<len&&k<len)
    	{
    		int t=s[(i+k)%len]-s[(j+k)%len];
    		if(!t)
    			++k;
    		else
    		{
    			if(t>0)
    				i+=(k+1);
    			else
    				j+=(k+1);
    			if(i==j)
    				++j;
    			k=0;
    		}
    	}
    	return i<j?i:j;
    }
    int maxp()
    {
    	int i=0,j=1,k=0;
    	while (i<len&&j<len&&k<len)
    	{
    		int t=s[(i+k)%len]-s[(j+k)%len];
    		if(!t)
    			++k;
    		else
    		{
    			if(t>0)
    				j+=(k+1);//要把这里i、j互换即可,其他不变
    			else
    				i+=(k+1);
    			if(i==j)
    				++j;
    			k=0;
    		}
    	}
    	return i<j?i:j;
    }
    pii kmp_count(char s[],char p[],int nxt[])
    {
    	int i=0,j=0;
    	pii r;
    	r.first=-1,r.second=0;
    	while (i<L-1&&j<len)
    	{
    		if(s[i]==p[j]||j==-1)
    		{
    			++i;
    			++j;
    		}
    		else
    			j=nxt[j];
    		if(j==len)
    		{
    			if(r.first==-1)//只记录第一次的出现位置
    				r.first=i-j+1;
    			++r.second;
    			j=nxt[j];
    		}
    	}
    	return r;
    }
    int main(void)
    {
    	int i,j,k;
    	while (~scanf("%s",s))
    	{
    		len=strlen(s);
    		L=len<<1;
    		int minm=minp();
    		int maxm=maxp();
    		strcpy(temp,s);
    		strcat(temp,s);
    		int cnt;
    		for (cnt=0,i=minm; cnt<len; ++i,++cnt)
    			fir[cnt]=temp[i];
    		for (cnt=0,i=maxm; cnt<len; ++i,++cnt)
    			las[cnt]=temp[i];
    		getnext(fir,nxta);
    		getnext(las,nxtb);
    		pii a=kmp_count(temp,fir,nxta);
    		pii b=kmp_count(temp,las,nxtb);
    		printf("%d %d %d %d
    ",a.first,a.second,b.first,b.second);
    		init();
    	}
    	return 0;
    }
  • 相关阅读:
    【学习笔记】【C语言】注释
    【学习笔记】【C语言】标识符
    【学习笔记】【C语言】关键字
    【学习笔记】【C语言】第一个C程序
    【学习笔记】Xcode常见设置
    【学习笔记】Mac OS X系统介绍
    【学习笔记】虚拟机安装Mac系统
    javascript 对象 + 数组
    SpringMVC实现原理及详解
    javaweb国际化
  • 原文地址:https://www.cnblogs.com/Blackops/p/5805890.html
Copyright © 2020-2023  润新知