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 } 

  • 相关阅读:
    FileReader:读取本地图片文件并显示
    uploadfy插件结合php案例
    php 生成二维码,图片上传到又拍云
    php get/post 请求(可用于请求api)获取手机号码归属地
    php中curl的详细解说
    聊聊Web App、Hybrid App与Native App的设计差异
    我的前端之路
    使用angular.js开发的一个简易todo demo
    在线个人简历(续)
    在线个人简历
  • 原文地址:https://www.cnblogs.com/acvc/p/3915535.html
Copyright © 2011-2022 走看看