A, KMP + maximum and minimum notation
Maximum and minimum representation
Start time for Solving the maximum-minimum representation isomorphism string, in which complexity is $ O (n) $ was determined in a series string lexicographically maximum (minimum) of all the strings isomorphic position.
application:
- $ $ N-cycles is given string is determined how many different strings: one by one with a maximum (minimum) notation, then added \ (the Set \) deduplication
- All isomorphic string loop string lexicographically maximum (minimum) represents: represents a maximum (minimum) method obtains the starting position, to the output
- Determining whether the two strings isomorphic: a string representation of the two maximum (minimum) represented by the method, and then by-character comparison
principle:
Setting a character string \ (S \) , and $ S ' \ (a \) minimum $ S isomorphic cyclic strings, said strings then for a minimum cycle isomorphic notation, which is a real problem seeking string S a position, the output from this position to start the cycle \ (S \) , obtained $ S '$ lexicographically smallest.
The most simple method is to set I $ \ (, \) J \ (two pointers, \) I \ (indicated position of the minimum point, \) J $ pointer as a comparison.
Order \ (= 0 I \) , \ (. 1 J = \) , then:
- 若 \(S[i]>S[j]\),则:\(i=j\),\(j=i+1\)
- If \ (S [I] <S [J] \) , then: \ (J ++ \)
- If \ (S [I] = S [J] \) , then let pointer \ (K \) , respectively, and from $ $ I $ J \ (Comparative down position until \) S [I]! = S [ j] $
- 若 \(S[i+k]>S[j+k]\),则:\(i=j\),\(j=i+1\)
- Otherwise, $ j ++ $
Finally, to return $ i $
As can be seen, the simple algorithm S $ [I] = S [J] \ (when, \) I $ pointer moves too little, in the face as $ bbb ... bbbbbba $ string such complex, time complexity may be deteriorated to \ (O (^ n-2) \) , obtained for this problem to be improved \ (O (n) \) algorithm is the smallest representation of its core idea is $ S [i] while maintaining = S [j] $ when \ (I \) , two pointers $ $ J
Similarly Order \ (= 0 I \) , \ (. 1 J = \) , then:
- 若 \(S[i]>S[j]\),则:\(i=j\),\(j=i+1\)
- If \ (S [I] <S [J] \) , then: \ (J ++ \)
- If S $ [I] = S [J] \ (, then let pointer \) K \ (, respectively, from \) I \ (and \) J $ position comparison down until \ (S [i]! = S [j] \)
- 若 \(S[i+k]>S[j+k]\),则:\(i=i+k\)
- Otherwise \ (j ++ \)
Finally, return $ i $ and $ j $ small person can
1, the minimum value notation
int minmumRepresentation(char *str) //最小表示法
{
int len=strlen(str);
int i=0; //指向字符串最小的位置
int j=1; //比较指针
int k=0; //str[i]与str[j]相等时一次移动几个
while(i<len&&j<len&&k<len)
{
int temp=str[(i+k)%len]-str[(j+k)%len];//比较值的长度
if(temp==0)
k++;
else
{
if(temp>0) //维护i
i=i+k+1;
else //维护j
j=j+k+1;
if(i==j) //相等时比较指针后移
j++;
k=0;
}
}
return i<j?i:j; //返回i、j中较小的那个
}
2, the maximum value notation
int maxmumRepresentation(char *str) //最大表示法
{
int len=strlen(str);
int i=0; //指向字符串最小的位置
int j=1; //比较指针
int k=0; //str[i]与str[j]相等时一次移动几个
while(i<len&&j<len&&k<len)
{
int temp=str[(i+k)%len]-str[(j+k)%len];//比较值的长度
if(temp==0)
k++;
else
{
if(temp>0) //维护i
j=j+k+1;
else //维护j
i=i+k+1;
if(i==j) //相等时比较指针后移
j++;
k=0;
}
}
return i<j?i:j; //返回两者最小值
}
HDU-3374 problem-solving ideas
First, it is determined whether the cycle is a string, then the minimum and maximum notation notation to identify a position corresponding to a minimum, such a representation can identify a string loop to find the start of a character, then the string lexicographically smallest
Code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-9
#define INF 0x3f3f3f3f
#define LL long long
const int MOD=10007;
const int N=1000000+5;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;
int Next[N];
char str[N];
void getNext(char p[])
{
Next[0]=-1;
int len=strlen(p);
int j=0;
int k=-1;
while(j<len)
{
if(k==-1||p[j]==p[k])
{
k++;
j++;
Next[j]=k;
}
else
{
k=Next[k];
}
}
}
int minmumRepresentation(char *str) //最小表示法
{
int len=strlen(str);
int i=0;
int j=1;
int k=0;
while(i<len&&j<len&&k<len)
{
int temp=str[(i+k)%len]-str[(j+k)%len];
if(temp==0)
k++;
else
{
if(temp>0)
i=i+k+1;
else
j=j+k+1;
if(i==j)
j++;
k=0;
}
}
return i<j?i:j;
}
int maxmumRepresentation(char *str) //最大表示法
{
int len=strlen(str);
int i=0;
int j=1;
int k=0;
while(i<len&&j<len&&k<len)
{
int temp=str[(i+k)%len]-str[(j+k)%len];
if(temp==0)
k++;
else
{
if(temp>0)
j=j+k+1;
else
i=i+k+1;
if(i==j)
j++;
k=0;
}
}
return i<j?i:j;
}
int main()
{
while(scanf("%s",str)!=EOF)
{
getNext(str);
int n=strlen(str);
int len=n-Next[n];
int num=1;//数量
if(n%len==0)
num=n/len;
int minn=minmumRepresentation(str);//最小表示
int maxx=maxmumRepresentation(str);//最大表示
printf("%d %d %d %d\n",minn+1,num,maxx+1,num);
}
return 0;
}