Ghost [manacher]

  This question comes from the weekend exam questions of five20.

  

Description

  Given a 0/1 sequence, find the number of substrings that satisfy the " ⺉ " property.
  " ⺉ " property explanation:
  The substring of " ⺉ " property satisfies: first invert the substring, and then invert the number on each bit (ie, 0 becomes 1, 1 becomes 0), the new string and the original string are obtained. same. (
  Example: 1010 satisfies the "⺉" property, because it first becomes 0101, and then reverses it to 1010, which is the same as the original string.) The first line of
Input Format
  contains only one n, which represents the length of the 0/1 sequence.
  The second row is a 0/1 sequence of length n.
Output Format
  A number that represents the number of subsequences in the 0/1 sequence that satisfy the "⺉" property (modulo 233333333 because the answer may be very large).
Sample
  Input
  7
  1011001
  Output
  6
Explana on
  The 6 subsequences satisfying the " ⺉ " property in the sample are: 2 10 , 2 01 , 1 1100 , and 1 011001 . Subtasks
  may be too toxic because of the problem, if it can't completely solve the problem, You can try to solve some subtasks.
Test Points N
1 N ≤ 1000
2 N ≤ 1000
3 N ≤ 1000
4 N ≤ 1000
5 N ≤ 10000
6 N ≤ 10000
7 N ≤ 10000
8 N ≤ 10000
9 N = 520520
10 N = 520520
11 N = 520520
12 N = 520520
13 N = 2333333
14 N = 2333333
15 N = 2333333
16 N = 2333333
17 N = 6666666
18 N = 6666666
19 N = 10000000
20 N = 10000000


  Analysis: At the time of the exam, I thought it was a digital DP problem (and I didn't learn manacher at the time, and I wasn't very good at string algorithms), because Konjac was really not good at DP, so I fought violently 25 Scored, and then finished the five20 test and said that this is a manager algorithm question. Then I learned a manager today and got this question. In fact, careful analysis of this problem is simpler than the general manager. Change the judgment condition to not equal, so that even the inserted characters in the middle are not needed, and a for loop will cover the main body of the manager.

  Code:

 

 1 //It is made by HolseLee on 30th Apr 2018
 2 //five20's test
 3 #include<bits/stdc++.h>
 4 using namespace std;
 5 const int mod=233333333;
 6 const int N=1e7+7;
 7 long long n,ans,p[N];char s[N];
 8 int main()
 9 {
10   freopen("ghost.in","r",stdin);
11   freopen("ghost.out","w",stdout);
12   scanf("%lld%s",&n,s+1);long long id,mx=0;
13   for(int i=1;i<=n;i++){
14     if(i<mx)p[i]=min(p[id*2-i],mx-i);
15     while(i+p[i]+1<=n&&i-p[i]>=1&&s[i+p[i]+1]!=s[i-p[i]])p[i]++;
16     if(p[i]+i>mx)id=i,mx=p[i]+i;
17     ans=(ans+p[i])%mod;}
18   printf("%lld",ans);return 0;
19 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325077399&siteId=291194637