france

https://github.com/francecil

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 Problem C Count Good Substrings

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

bbbaab

 Sample Output

1 22 4

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;
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2014-08-04 08:53  france  阅读(232)  评论(0编辑  收藏  举报