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 }

 

转载于:https://www.cnblogs.com/Xiper/p/4467784.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值