zoukankan      html  css  js  c++  java
  • 1040 Longest Symmetric String

    Given a string, you are 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 output 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

    题意:

      找出最长的对称子串。

    思路:

      遍历字符串,以字符串中的每一个字符为对称中心,向两边查找。因为不知道对称子串的长度是奇数还是偶数,所以两种情况都要考虑,取最大值。

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main() {
     6     string str;
     7     getline(cin, str);
     8     int p1, p2, temp, ans = 1;
     9     int len = str.length();
    10     for (int i = 0; i < len; ++i) {
    11         p1 = i;
    12         p2 = i + 1;
    13         temp = 0;
    14         while (p1 >= 0 && p2 < len) {
    15             if (str[p1--] == str[p2++])
    16                 temp += 2;
    17             else
    18                 break;
    19         }
    20         if (temp > ans) ans = temp;
    21     }
    22     for (int i = 0; i < len; ++i) {
    23         p1 = p2 = i;
    24         temp = -1;
    25         while (p1 >= 0 && p2 < len) {
    26             if (str[p1--] == str[p2++])
    27                 temp += 2;
    28             else
    29                 break;
    30         }
    31         if (temp > ans) ans = temp;
    32     }
    33     cout << ans << endl;
    34     return 0;
    35 }
  • 相关阅读:
    JS 原型模式 工厂模式 构造函数的区别
    JS 深入1
    理解DOM的一个例子
    Fuzzing参数
    神经网络相关知识和概念整理
    [转载] 系统、模型和仿真
    frp内网穿透,从外网访问内网资源
    常用软件配置
    141. 环形链表
    501. 二叉搜索树中的众数
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12873054.html
Copyright © 2011-2022 走看看