Ural 1297 palindrome (longest echo substring)

Source: Internet
Author: User

This topic is now linear with manacherAlgorithmWrite, suffix array version has not been implemented, it is planned to be done in two days

I still think it is both easy to write and fast to solve the retrieval problem.

# Include <iostream> # include <string. h> # include <stdio. h> using namespace STD; # define min (a, B) (a <B? A: B) Char STR [2500]; char rec [2500]; int Drome [2500]; int main () {int I, j, k; int max_pos, Pos; while (scanf ("% s", REC )! = EOF) {k = 0; STR [k ++] = '@'; for (I = 0; REC [I]; I ++) {STR [k ++] = '#'; STR [k ++] = rec [I];} STR [k ++] = '#'; STR [k] = 0; Drome [0] = 1; max_pos = 0; Pos = 0; for (I = 1; STR [I]; I ++) {If (max_pos> I) {Drome [I] = min (Drome [POS * 2-I], max_pos-i);} else Drome [I] = 1; (; STR [I + Drome [I] = STR [I-Drome [I]; Drome [I] ++); if (I + Drome [I]> max_pos) pos = I, max_pos = Drome [I] + I;} max_pos =-1; Pos = 0; For (j = 0; j <I; j ++) {If (Drome [J]> max_pos) {Max_pos = Drome [J]; Pos = J ;}// printf ("MAX: % d \ n", max_pos); max_pos; k = pos-max_pos + 1; pos + = max_pos-1; For (K; k <Pos; k ++) if (STR [k]! = '#') Printf ("% C", STR [k]); printf ("\ n");} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.