zoukankan      html  css  js  c++  java
  • URAL 2040 Palindromes and Super Abilities 2

    Palindromes and Super Abilities 2
    Time Limit: 500MS Memory Limit: 102400KB 64bit IO Format: %I64d & %I64u

    Description

    Dima adds letters s1, …, sn one by one to the end of a word. After each letter, he asks Misha to tell him how many new palindrome substrings appeared when he added that letter. Two substrings are considered distinct if they are different as strings. Which n numbers will be said by Misha if it is known that he is never wrong?

    Input

    The input contains a string s1 … sn consisting of letters ‘a’ and ‘b’ (1 ≤ n ≤ 5 000 000).

    Output

    Print n numbers without spaces: i-th number must be the number of palindrome substrings of the prefix s1 … si minus the number of palindrome substrings of the prefix s1 … si−1. The first number in the output should be one.
     
    Sample Input
    abbbba
     
    Sample Output
    111111

    Notes

    We guarantee that jury has C++ solution which fits Time Limit at least two times. We do not guarantee that solution on other languages exists (even Java).

    Source

    Problem Author: Mikhail Rubinchik (prepared by Kirill Borozdin) 
     
    解题:PalindromicTree
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 5000010;
     4 struct PalindromicTree{
     5     struct node{
     6         int son[2],f,len;
     7         void init(int len){
     8             memset(son,0,sizeof son);
     9             this->len = len;
    10         }
    11     }e[maxn];
    12     int tot,last,n;
    13     char s[maxn];
    14     int newnode(int len = 0){
    15         e[tot].init(len);
    16         return tot++;
    17     }
    18     int getFail(int x){
    19         while(s[n - e[x].len - 1] != s[n]) x = e[x].f;
    20         return x;
    21     }
    22     void init(){
    23         last = tot = n = 0;
    24         newnode(0);
    25         newnode(-1);
    26         e[0].f = 1;
    27         s[n] = -1;
    28     }
    29     bool extend(int c){
    30         s[++n] = c;
    31         int cur = getFail(last);
    32         bool isExtend = e[cur].son[c] == 0;
    33         if(!e[cur].son[c]){
    34             int newson = newnode(e[cur].len + 2);
    35             e[newson].f = e[getFail(e[cur].f)].son[c];
    36             e[cur].son[c] = newson;
    37         }
    38         last = e[cur].son[c];
    39         return isExtend;
    40     }
    41 }pt;
    42 char str[maxn];
    43 int main(){
    44     while(gets(str)){
    45         pt.init();
    46         for(int i = 0; str[i]; ++i){
    47             if(pt.extend(str[i] - 'a')) putchar('1');
    48             else putchar('0');
    49         }
    50         putchar('
    ');
    51     }
    52     return 0;
    53 }
    View Code
     
  • 相关阅读:
    玩不转云计算的架构
    从《从架构的角度看,如何写好代码?》中来看如何编写单元测试代码
    换种形式工作
    程序员下一门要学的编程语言Swift
    从钉钉微应用定制化导航栏看如何实现Hybrid App开发框架
    纯灌水Linus主义
    kFreeBSD有活过来的迹象?UbuntuBSD
    架构的重要性
    MacOS下如何进行Git的冲突(Conflict)处理
    [转]以Facebook为案例剖析科技公司应有的工具文化
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4913873.html
Copyright © 2011-2022 走看看