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.
  • 相关阅读:
    【源码学习之spark core 1.6.1 standalone模式下的作业提交】
    【源码学习之spark streaming 1.6.1 】
    spark udf 初识初用
    spark 累加历史 + 统计全部 + 行转列
    spark 都用了哪些开源东东
    kafka 官方示例代码--消费者
    104. 二叉树的最大深度
    237. 删除链表中的节点
    Leetcode-167. Two Sum II
    Leetcode-215. Kth Largest Element in an Array
  • 原文地址:https://www.cnblogs.com/Xiper/p/4467784.html
Copyright © 2011-2022 走看看