Accept: 11 Submit: 23
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, "aabba" is good, because after the merging step it will become "aba".
Given a string, you have to find two values:
1. the number of good substrings of even length;
2. the number of good substrings of odd length.
Input
There are several test cases.( <= 20)
The first line of the input contains a single string of length n (1 <= n <= 10^5). Each character of the string will be either 'a' or 'b'.
Output
For each test case, print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.
Sample Input
Sample Output
Cached at 2014-08-04 00:34:05.
由于只有a,b 将字符串压缩, 如 abbbaaabb 变成 1332 枚举做
#include<iostream> #include<algorithm> #include<string> using namespace std; int min(int a,int b){ return a>b?b:a; } int main(){ string str; int ji;int ou;int i,j; int num[10010]; while(cin>>str){ ji=0;ou=0; int len=str.size();int tt=1;int temp=1; for(i=1;i<len;i++){ if(str[i]==str[i-1])temp++; else{num[tt++]=temp;temp=1;} } num[tt]=temp; // for(j=1;j<=tt;j++){ // 1 // cout<<num[j]<<" "; // } for(i=1;i<=tt;i++){ for( j=0;j<=tt;j++){ if(i-j<=0||i+j>tt)break; if(j==0){ if(num[i]%2==0){ int sum1=(1+(num[i]-1))*(num[i]/2)/2; int sum12=(2+num[i])*(num[i]/2)/2; // cout<<"1ou:"<<sum1<<endl; // cout<<"1ji:"<<sum12<<endl; ji+=sum12; ou+=sum1; } else{ int sum2=(1+num[i])*((num[i]+1)/2)/2; int sum21=(2+num[i]-1)*(num[i]/2)/2; // cout<<"1ou:"<<sum21<<endl; // cout<<"1ji:"<<sum2<<endl; ji+=sum2; ou+=sum21; } continue; } if(num[i]%2==1){ if(num[i-j]==num[i+j]) ji+=num[i-j]; else { ji+=min(num[i-j],num[i+j]);break;} } if(num[i]%2==0){ // ou+=num[i]; if(num[i-j]==num[i+j])ou+=num[i-j]; else {ou+=min(num[i-j],num[i+j]);break;} } } } cout<<ou<<" "<<ji<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。