zoukankan      html  css  js  c++  java
  • 2015 Multi-University Training Contest 7 hdu 5371 Hotaru's problem

    Hotaru's problem

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 907    Accepted Submission(s): 322


    Problem Description
    Hotaru Ichijou recently is addicated to math problems. Now she is playing with N-sequence.
    Let's define N-sequence, which is composed with three parts and satisfied with the following condition:
    1. the first part is the same as the thrid part,
    2. the first part and the second part are symmetrical.
    for example, the sequence 2,3,4,4,3,2,2,3,4 is a N-sequence, which the first part 2,3,4 is the same as the thrid part 2,3,4, the first part 2,3,4 and the second part 4,3,2 are symmetrical.

    Give you n positive intergers, your task is to find the largest continuous sub-sequence, which is N-sequence.
     
    Input
    There are multiple test cases. The first line of input contains an integer T(T<=20), indicating the number of test cases. 

    For each test case:

    the first line of input contains a positive integer N(1<=N<=100000), the length of a given sequence

    the second line includes N non-negative integers ,each interger is no larger than 109 , descripting a sequence.
     
    Output
    Each case contains only one line. Each line should start with “Case #i: ”,with i implying the case number, followed by a integer, the largest length of N-sequence.

    We guarantee that the sum of all answers is less than 800000.
     
    Sample Input
    1
    10
    2 3 4 4 3 2 2 3 4 4
     
    Sample Output
    Case #1: 9
     
    Source
     
    解题:manacher暴力搞,fread真是TLE的救星啊
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 201000;
     4 int s[maxn],p[maxn];
     5 int manacher(int n) {
     6     int id = 0,maxlen = 0;
     7     s[0] = -1;
     8     for(int i = n; i >= 0; --i) {
     9         s[i + i + 2] = s[i];
    10         s[i + i + 1] = 0;
    11     }
    12     n = (n<<1|1);
    13     for(int i = 2; i < n; ++i) {
    14         if(p[id] + id > i) p[i] = min(p[2*id-i],p[id]+id-i);
    15         else p[i] = 1;
    16         while(s[i-p[i]] == s[i+p[i]]) ++p[i];
    17         if(id + p[id] < i + p[i]) id = i;
    18         if(maxlen < p[i]) maxlen = p[i];
    19     }
    20     return maxlen - 1;
    21 }
    22 char *ch, *ch1, buf[50*1024000+5], buf1[50*1024000+5];
    23 void read(int &x) {
    24     for (++ch; *ch <= 32; ++ch);
    25     for (x = 0; '0' <= *ch; ch++)    x = x * 10 + *ch - '0';
    26 }
    27 int main() {
    28     int kase,n,cs = 1;
    29     ch = buf - 1;
    30     ch1 = buf1 - 1;
    31     fread(buf, 1, 1000 * 45 * 1024, stdin);
    32     read(kase);
    33     while(kase--) {
    34         read(n);
    35         for(int i = 0; i < n; ++i)
    36             read(s[i]);
    37         manacher(n);
    38         int ret = 0;
    39         n = (n<<1|1);
    40         for(int i = 3; i < n; i += 2) {
    41             if(p[i] - 1 > ret) {
    42                 int r = p[i] + 1;
    43                 while(r > ret && p[i + r] < r) --r;
    44                 if(ret < r) ret = r;
    45             }
    46         }
    47         printf("Case #%d: %d
    ",cs++,(ret>>1)*3);
    48     }
    49     return 0;
    50 }
    51 /*
    52 1
    53 11
    54 1 1 2 1 1 2 1 1 2 1 1
    55 */
    View Code
     
  • 相关阅读:
    [leetcode-495-Teemo Attacking]
    [leetcode-413-Arithmetic Slices]
    document对象操作:浏览器页面文件
    搭建wamp环境,数据库基础知识
    jenkins配置邮箱服务器(126邮箱)
    Linux命令之文件与用户权限
    并发与同步、信号量与管程、生产者消费者问题
    TypeScript设计模式之职责链、状态
    了解HTML列表
    CSS画出的图
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4723405.html
Copyright © 2011-2022 走看看