CodeForces - 1238D (thinking)

The meaning of problems

https://vjudge.net/problem/CodeForces-1238D

If each letter of a string, at least a part of (length greater than 1) of the palindrome string, this string is called good.

How many good string of a substring of a string of length n s (only by the letters A, B composition), Q s there is

Thinking

It found that only XYX This staggered string or XX ... X it may be good string.

Direct comparison difficult, we do not consider seeking legal string.

Therefore, we iterate over the first n-string, XXXXY find this, i.e. by s [i]! = S [i-1] in front of the same number of characters is calculated, the number string that is not legal (XY, XXY , XXXY, XXXXY not legitimate)

Then traversed backwards again, to find that XYYYY, i.e. by s [i]! = S [i + 1] is calculated in the same character after Ge prime, the number of strings that is not legal (XY, XYY, XYYY, XYYYY )

But we can also find that doing so will reduce this XY twice, the fact is that being a math became XY, calculate backwards became YX, so re-iterate again, the XY This staggered the number counted out.

A string of length 1 would be illegal, so the total string (n-1 + 1) * (n-1) / 2 minus the above circumstances illegal, plus multiple subtraction XY.

Code

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int main ()
{
    std::ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    string s;
    cin>>s;
    ll years = n * (n-1) / 2, cnt = 1;
    for(int i=1;i<n;i++)//XXXXY
    {
        if(s[i]==s[i-1]) cnt++;
        else ans- = cnt, cnt = 1;
    }
    cnt=1;
    for(int i=n-2;i>=0;i--)
    {
        if(s[i]==s[i+1]) cnt++;
        else ans- = cnt, cnt = 1;
    }
    for(int i=1;i<n;i++)
        if(s[i]!=s[i-1])
            ++ years;
    cout<<ans<<endl;
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/mcq1999/p/11980369.html