pat1040. Longest symmetric String (25)

Source: Internet
Author: User
Tags cmath

1040. Longest symmetric String (25) time limit of MS memory limit 65536 KB code length limit 16000 B procedure StandardAuthor Chen, Yue

Given A string, you is supposed to output the length of the longest symmetric sub-string. For example, given ' is pat&tap symmetric? ', the longest symmetric sub-string is ' s pat&tap s ', hence you must OUTP UT 11.

Input Specification:

Each input file contains one test case which gives a non-empty string of length No. more than 1000.

Output Specification:

For each test case, simply print the maximum length in a line.

Sample Input:
Is Pat&tap symmetric?
Sample Output:
11

Submit Code

Method One: Insert invalid characters, traverse once.

1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <queue>6#include <vector>7#include <cmath>8#include <string>9#include <map>Ten#include <Set> One using namespacestd; A intdp[20005]; - intMain () { -     //freopen ("D:\\input.txt", "R", stdin); the     //scanf ("%s", s); -     strings; - getline (cin,s); -  +     //cout<<s<<endl; -  +     inti,j=0, k,count=0; Adp[j++]=-1; at      for(i=0; I<s.length (); i++){ -Dp[j++]=s[i];//Hash -dp[j++]=-1; -     } -  -     //cout<<j<<endl; in  -      for(i=1; i<j;i++) {//I start from 1!!  to         intf=i-1, b=i+1; +          while(f>=0&&b<j&&dp[f]==Dp[b]) { -f--; theb++; *         } $         if(count<b-f-1){Panax Notoginsengcount=b-f-1; -             //cout<<count<<endl; the         } +     } Aprintf"%d\n", count/2);//here can be divided into the center for-1 and the normal number of 2 case discussion the     return 0; +}

Method Two: Sub-parity discussion:

1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <queue>6#include <vector>7#include <cmath>8#include <string>9#include <map>Ten#include <Set> One using namespacestd; A intMain () { -     //freopen ("D:\\input.txt", "R", stdin); -     //scanf ("%s", s); the     strings; - getline (cin,s); -  -     //cout<<s<<endl; +  -     intI,j,k,count=0; +      for(i=0; I<s.length (); i++){ A         intf=i,b=i; at          while(f>=0&&b<s.length () &&s[f]==S[b]) { -f--; -b++; -         } -         if(count<b-f-1){ -count=b-f-1; in         } -f=i; tob=i+1; +          while(f>=0&&b<s.length () &&s[f]==S[b]) { -f--; theb++; *         } $         if(count<b-f-1){Panax Notoginsengcount=b-f-1; -         } the     } +printf"%d\n", count); A     return 0; the}

pat1040. Longest symmetric String (25)

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.