zoukankan      html  css  js  c++  java
  • 求奇偶子回文串个数

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





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

    today lazy . tomorrow die .
  • 相关阅读:
    数据库——大事务
    数据库——性能理解
    java——通过GenericObjectPool获取到的资源,调用close()方法会close还是returnObject?
    gradle——入门
    MongoDB——morphia
    jvm——Java main方法的执行
    sql——limit
    阿里云Open API自动化脚本—ECS公网IP转化弹性公网IP
    阿里云共享带宽
    mysql5.x安装脚本
  • 原文地址:https://www.cnblogs.com/france/p/4808689.html
Copyright © 2011-2022 走看看