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.
  • 相关阅读:
    Android测试AsyncTask下载图片
    Android DatePickerDialog TimepickerDialog
    Android AlertDialog
    Android activity的回传数据
    Android Handler简单使用
    Java Switch(String)
    JAVA测试装饰者模式
    Java实现数组按数值大小排序
    Java参数按值传递?按引用传递
    Spring cloud之断路器hystrix包问题
  • 原文地址:https://www.cnblogs.com/Xiper/p/4467784.html
Copyright © 2011-2022 走看看