Magic Necklace
|
Problem Description
Mother's Day is coming, little H ready to give her a special necklace.
This string can be seen as a necklace with lowercase letters, lowercase letters each represent a color.
To make this necklace, small H purchased two machines.
The first machine may generate all forms of palindromic sequence, the second machine can be connected to two palindromic sequence, and the second machine there is a special property:
If a suffix string and a string of prefix is exactly the same, then this can be repeated partially overlap. For example: aba and aca connected, the string may be generated abaaca or abaca.
Now given target style necklace, you need to ask how many times a second machine in order to generate this special necklace.
Input Format
How the input data lines, each a string representing the target necklace style.
Output Format
Multiple lines, each line represents the minimum number of answers to a second machine is required.
Sample input 1
abcdcba
abacada
ABCDEF
Sample output 1
0
2
5
Sample input 2
xuqeytcixfzpzvcacymqncdohedfyowmipplplkyrsaspjliczflordhlbckyiuqxkslntofajs
amjmaekzbnbwagotspirvjksendltyeeuswefpdcdmmhzomlvkrhtwidlybkvvvebqkmvednaxddeygghrvqfaxwjssvcphcrzeauwlowwdmhacpzbnihgmbypfsblvsyaugkcg
Sample output 2
65
118
prompt
Each test data input less than 5 lines
String length of each row is less 50000
1. Identify all drawn cart through palindromic substrings, each of the palindromic regarded as a sub-string segment
2. The question becomes minimal is selected from the range covering the entire line segment, i.e., the minimum interval coverage problems. Greed can be.
#include <stdio.h> #include <bits / STDC ++ H.> the using namespace STD; char S [ 100010 ]; int len [ 100010 ]; // palindromic sequence length int n-; struct the Node { int L, R & lt; } A [ 100010 ]; BOOL CMP (the Node A, the Node B) // the left end greedy ordering { return Al < BL; } void the Init () { for ( int I = n-; I> = . 1 ; i-- ) { S [i <<1]=s[i]; s[i<<1|1]='&'; } n=n<<1|1; s[0]='%'; s[1]='&'; s[n+1]='^'; } void manacher() { int maxright=0,num=0; for(int i=1; i<=n; i++) { if(i<maxright) len[i]=min(len[num*2-i],maxright-i); else len[i]=1; while(s[i+len[i]]==s[i-len[i]]) { len[i]++; } if(maxright<i+len[i]) { maxright=i+len[i]; num=i; } } } intmain () { the while (Scanf ( " % S " , S + . 1 ) = the EOF!) // starts within the operator { n- = strlen (S + . 1 ); the Init (); manacher (); for ( int I = . 1 ; i <= n-; i ++ ) { a [i] .L = i-len [i] + . 1 ; // in length i is the center of the left a [i] = i + len .r [i] - . 1 ; // at the center of the right length i is } Sort (a + . 1 , a + . 1 + n-, CMP); int r=0,ans=0,i=1; while(i<=n) { int sum=0; while(a[i].l-1<=r&&i<=n) { sum=max(sum,a[i].r); i++; } ans++;//现长 r=sum;//覆盖完全 if(r==n) { break; } //++ years; } Cout << years- 1 << endl; } }