• 求奇偶子回文串个数


     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 .
  • 相关阅读:
    js 学习的地址;
    HTML onmousedown 事件属性 关于鼠标按键的问题;
    contextmenu="supermenu" 属性的应用 右键菜单打开和保存功能
    gulp自动化ES6转ES5
    angularjs获取元素以及angular.element()用法
    gulp学习总结
    Git 代码管理常用命令
    SpringMVC基于代码的配置方式(零配置,无web.xml)
    [Android Studio 权威教程]AS 中配置强大的版本号管理系统(Git、SVN、等)
    SVM支持向量机
  • 原文地址:https://www.cnblogs.com/france/p/4808689.html
Copyright © 2020-2023  润新知