zoukankan      html  css  js  c++  java
  • CF Round 427 D. Palindromic characteristics

    题目链接:http://codeforces.com/contest/835/problem/

    思路:dp[i][j]表示子串[i, j]的阶数,则:

    dp[i][i] = 1;

    dp[i][i + 1] = (str[i] == str[i + 1])? 2: 0;

    if(str[i] != str[j] || dp[i + 1][j - 1] == 0)

      dp[i][j] = 0;
    else  

      dp[i][j] = dp[i][i + (j - i + 1) / 2 - 1] + 1;

    而所有的k阶回文串必定是k - 1, k - 2....1阶回文川,因此 cnt[i] += sum(cnt[i + 1 -> len])

    代码:

     1 const int inf = 0x3f3f3f3f;
     2 const int maxn = 5e3 + 5; 
     3 
     4 int dp[maxn][maxn], cnt[maxn];
     5 char str[maxn];
     6 
     7 int main(){
     8     memset(dp, 0, sizeof(dp));
     9     memset(cnt, 0, sizeof(cnt));
    10     scanf("%s", &str[1]);
    11     int slen = strlen(str + 1);
    12     for(int i = 1; i <= slen; i++) dp[i][i] = 1;
    13     for(int i = slen - 1; i > 0; i--){
    14         dp[i][i + 1] = str[i] == str[i + 1]? 2: 0;
    15         for(int j = i + 2; j <= slen; j++){
    16             if(str[i] != str[j] || dp[i + 1][j - 1] == 0) dp[i][j] = 0;
    17             else dp[i][j] = dp[i][i + (j - i + 1) / 2 - 1] + 1;
    18         }
    19     }
    20     for(int i = 1; i <= slen; i++){
    21         for(int j = i; j <= slen; j++){
    22 //            debug
    23 //            printf("[%d, %d]: %d
    ", i, j, dp[i][j]);
    24             cnt[dp[i][j]]++;
    25         }
    26     }
    27     for(int i = 1; i <= slen; i++){
    28         for(int j = i + 1; j <= slen; j++){
    29             cnt[i] += cnt[j];
    30         }
    31     }
    32     for(int i = 1; i <= slen; i++){
    33         printf("%d", cnt[i]);
    34         if(i != slen) putchar(' ');
    35         else puts("");
    36     }
    37 }

    题目:

    D. Palindromic characteristics
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.

    A string is 1-palindrome if and only if it reads the same backward as forward.

    A string is k-palindrome (k > 1) if and only if:

    1. Its left half equals to its right half.
    2. Its left and right halfs are non-empty (k - 1)-palindromes.

    The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string tdivided by 2, rounded down.

    Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.

    Input

    The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters.

    Output

    Print |s| integers — palindromic characteristics of string s.

    Examples
    input
    abba
    output
    6 1 0 0 
    input
    abacaba
    output
    12 4 1 0 0 0 0 
    Note

    In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here.

  • 相关阅读:
    让他人变得优秀是伟大
    年龄越大,离家越远
    秋天的叶子
    [短彩信]C#短彩信模块开发设计(2)——配置
    jquery实现网页二级菜单简单代码
    HTML页面做中间页跳转传递参数
    ToJson
    Button页面中的按钮
    SQLSERVER 18056 错误
    在桌面添加可拖动/点击的悬浮窗口
  • 原文地址:https://www.cnblogs.com/bolderic/p/7270337.html
Copyright © 2011-2022 走看看