zoukankan      html  css  js  c++  java
  • Codeforces Round #258 (Div. 2) D

    D. Count Good Substrings
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output


    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

    The first line of the input contains a single string of length n (1 ≤ n ≤ 105). Each character of the string will be either 'a' or 'b'.

    Output

    Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.

    Sample test(s)
    input
    bb
    output
    1 2
    input
    baab
    output
    2 4
    input
    babb
    output
    2 5
    input
    babaa
    output
    2 7

     sl :分析发现最后回文串的第一个字符和第二个字符相同,这样统计相应数位上的字符就行了。

     1 //by caonima
     2 //hehe
     3 #include <bits/stdc++.h>
     4 typedef long long LL;
     5 const int MAX = 1e5+10;
     6 char str[MAX];
     7 LL even_cnt[2],odd_cnt[2];
     8 // odd ji even o
     9 int main() {
    10     LL odd,even;
    11     while(scanf("%s",str+1)==1) {
    12         memset(even_cnt,0,sizeof(even_cnt));
    13         memset(odd_cnt,0,sizeof(odd_cnt));
    14         int n=strlen(str+1);
    15         odd=even=0;
    16         for(int i=1;i<=n;i++) {
    17             int x=str[i]-'a';
    18             if(i&1) {
    19                 odd+=odd_cnt[x];
    20                 even+=even_cnt[x];
    21                 odd_cnt[x]++;
    22             }
    23             else {
    24                 odd+=even_cnt[x];
    25                 even+=odd_cnt[x];
    26                 even_cnt[x]++;
    27             }
    28         }
    29         odd+=n;
    30         printf("%I64d %I64d ",even,odd);
    31     }
    32     return 0;

    33 } 

  • 相关阅读:
    java中的socket编程有关printStream的println方法和write方法
    json在php中的用法
    js的数组处理函数splice
    将博客搬至CSDN
    mapreduce导出MSSQL的数据到HDFS
    基于信息熵的无字典分词算法
    搜索引擎手记(三)之网页的去重
    算法之常用的距离和相似度度量
    搜索引擎手记(二)之爬虫的开发
    搜索引擎手记(一)之引擎工作的开始
  • 原文地址:https://www.cnblogs.com/acvc/p/3915535.html
Copyright © 2011-2022 走看看