Substrings in

Here Insert Picture Description
Find the number of a substring in the string, if the enumeration of certain time-out, so we use the method of recording the number of occurrences of value with a time record c, after encountering h, h and let the front of a combine so all options c occurrence number tuple plus the current, once every encountered plus one, causes the current number plus answer n-tuple of the face can not clear all data cycles only sweep over O (n) can solve the problem
code is also very simple

#include <stdio.h>
int main()
{
    char a[10000];
    long long n=0,c=0,ch=0,i;
    gets(a);
    for(i=0;a[i]!='\0';i++)
    {
        if(a[i]=='C')
            c++;
        else if(a[i]=='H')
            ch=ch+c;
        else if(a[i]=='N')
            n=n+ch;
    }
    printf("%lld",n);
}
Published 48 original articles · won praise 17 · views 4456

Guess you like

Origin blog.csdn.net/weixin_45757507/article/details/104478670