Codeforces 1238 D. AB-string

The question of thinking a lot ah cf thinking title

Refers to the good string defines a string in which each character belongs to a length> palindromic portion of the string 2 =. Call thee a string a few good substring.

Obviously ab string of good string will be more. As long as aba, bab, aa, bb, and so it is easy to meet

A total of n * n characters of the substring (n-1) / 2 different locations, we consider the reverse is not excluded good string portion.

First thought ab, ba is not a good string, and they branch Shen aaaaaaaaab, bbbbbbbbba, abbbbbbbb, baaaaaaaa, this category is also not good string, and which contained the same length of string is extracted from this part of the non-good the number of string.

So you can find from front to back before two non-good string, and then find two non-good string back from the front after the last ab, ba this is subtracted twice, plus once.

 1 #include <bits/stdc++.h>
 2 #define debug(x) cout << #x << ": " << x << endl
 3 using namespace std;
 4 typedef long long ll;
 5 const int MAXN=2e5+7;
 6 const int INF=0x3f3f3f3f;
 7 
 8 int main()
 9 {
10     ios::sync_with_stdio(false);
11     cin.tie(0);
12     int n;
13     string s;
14     cin>>n>>s;
15     ll ans=1ll*n*(n-1)/2;
16     int cur=1;
17     for(int i=1;i<s.size();++i)
18     {
19         if(s[i]==s[i-1])
20             cur++;
21         else
22         {
23             ans-=cur;
24             cur=1;
25         }
26     }
27     cur=1;
28     for(int i=n-2;i>=0;--i)
29     {
30         if(s[i]==s[i+1])
31             cur++;
32         else
33         {
34             ans-=cur;
35             cur=1;
36         }
37     }
38     for(int i=0;i<s.size()-1;++i)
39         if(s[i]!=s[i+1]) ans++;
40     cout<<ans<<endl;
41     return 0;
42 }
View Code

 

Guess you like

Origin www.cnblogs.com/Zzqf/p/11653785.html
Recommended