zoukankan      html  css  js  c++  java
  • UESTC_How many good substrings CDOJ 1026

    Icerain likes strings very much. Especially the strings only consist of 0 and 1,she call them easy strings. One day, she is so boring that she want to find how many good substrings in an easy string?

    A good substring is a substring which can be a palindrome string after you change any two characters' positions(you can do this operation any times).For example,100 is a good substring of 1001,beacuse you can change it to be 010,which is a palindrome string.

    Input

    The first line is the number of test cases. (no more than 100)

    Each test case has one line containing one string only consist of 0 and 1. The length of the string is no greater than 100000.

    Output

    For each test case, output one line contains the number of good substrings.

    Sample input and output

    Sample InputSample Output
    2
    01
    01101
    2
    11

    解题报告

    前缀dp...感谢卿神指导..

     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 const int maxn = 1e5 + 5;
     6 int dp[4];
     7 char buffer[maxn];
     8 
     9 int main(int argc,char *argv[])
    10 {
    11   int Case;
    12   scanf("%d",&Case);
    13   while(Case--)
    14    {
    15          scanf("%s",buffer);
    16          int len = strlen(buffer);
    17          long long ans = 0;
    18          memset(dp,0,sizeof(dp));
    19          for(int i = 1 ; i <= len ; ++ i)
    20           {
    21                 int flag = 0;
    22                 if (buffer[i-1] == '1')
    23                  {
    24                        swap(dp[0],dp[1]);
    25                        swap(dp[2],dp[3]);
    26                        flag = 1;
    27            }
    28           else 
    29            {
    30                  swap(dp[0],dp[3]);
    31                  swap(dp[1],dp[2]);
    32            }
    33           if (flag)
    34            dp[2] ++ ;
    35           else
    36            dp[0] ++ ;
    37           ans += (dp[0] + dp[2] + dp[3]);
    38        }
    39       cout << ans << endl;
    40    }
    41   return 0;
    42 }
    No Pain , No Gain.
  • 相关阅读:
    10-JavaScript 条件语句
    9-JavaScript 比较
    8-JavaScript 运算符
    6-JavaScript 事件
    Sum Problem 重定向文件的使用
    Calculate A + B.
    Vue中computed的本质及与methods的区别
    外部文件使用django的models
    DjangoURL反向解析
    字符串格式化的方式
  • 原文地址:https://www.cnblogs.com/Xiper/p/4467784.html
Copyright © 2011-2022 走看看