zoukankan      html  css  js  c++  java
  • Calf Flac

    1.3.3 Calf Flac

    Time Limit: 1 Sec  Memory Limit: 64 MB
    Submit: 223  Solved: 42
    [Submit][Status][Forum]

    Description

    据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文。你的工作就是去寻找这些牛制造的奇观(最棒的回文)。 在寻找回文时不用理睬那些标点符号、空格(但应该保留下来以便做为答案输出),只用考虑字母'A'-'Z'和'a'-'z'。要你寻找的最长的回文的文章是一个不超过20,000个字符的字符串。 我们将保证最长的回文不会超过2,000个字符(在除去标点符号、空格之前)。

    Input

    输入文件不会超过20,000字符。这个文件可能一行或多行,但是每行都不超过80个字符(不包括最后的换行符)。

    Output

    输出的第一行应该包括找到的最长的回文的长度。 下一行或几行应该包括这个回文的原文(没有除去标点符号、空格),把这个回文输出到一行或多行(如果回文中包括换行符)。 如果有多个回文长度都等于最大值,输出最前面出现的那一个。

    Sample Input

    Confucius say: Madam, I'm Adam.

    Sample Output

    11
    Madam, I'm Adam

    线性寻找最长回文子串(给定回文字串最长的长度)。
    定义两个指针分别指向要查找字符串的头和尾,如果头和尾的字母一样,则两个指针都向前移动一,否则,将头指针拨回初始的地方,尾指针不动(如果头指针一开始就在头部的话,则尾指针向前移动一)。
    判断结束条件是:头指针的位置大于尾指针的位置。

    这个题最后的输出也挺巧妙的。

      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<cstring>
      4 #include<string>
      5 #include<cmath>
      6 #include<algorithm>
      7 #include<queue>
      8 #include<stack>
      9 #include<deque>
     10 #include<map>
     11 #include<iostream>
     12 using namespace std;
     13 typedef long long  LL;
     14 const double pi=acos(-1.0);
     15 const double e=exp(1);
     16 const int N = 200009;
     17  
     18 char con[N],name[N];
     19  
     20 int main()
     21 {
     22     int i=0,p,j=0,n,t;
     23     while(scanf("%c",&con[i])!=EOF)
     24     {
     25         if((con[i]>=65&&con[i]<=90)||(con[i]>=97&&con[i]<=122))
     26         {
     27             name[j++]=con[i];
     28         }
     29         i++;
     30     }
     31     int head,tail,mid=0,most=0;
     32     int spot=1;
     33  
     34     t=i-1;
     35     n=j-1;
     36  
     37     for(i=0; i<=n; i++)
     38     {
     39         mid=0;
     40         head=i;
     41         tail=i+1999;
     42         while(1)
     43         {
     44             if(tail>n)
     45                 tail=n;
     46             else
     47             {
     48                 if(head>tail)
     49                     break;
     50                 if(name[head]>=65&&name[head]<=90&&(name[head]==name[tail])||(name[head]+32==name[tail]))
     51                 {
     52  
     53                         if(head!=tail)
     54                             mid+=2;
     55                         else
     56                             mid+=1;
     57                         head++;
     58                         tail--;
     59  
     60                 }
     61                 else if(name[head]>=97&&name[head]<=122&&(name[head]==name[tail])||(name[head]-32==name[tail]))
     62                 {
     63                         if(head!=tail)
     64                             mid+=2;
     65                         else
     66                             mid+=1;
     67                         head++;
     68                         tail--;
     69                 }
     70                 else
     71                 {
     72                     if(head==i)
     73                         tail--;
     74  
     75                     head=i;
     76                     mid=0;
     77                 }
     78             }
     79         }
     80         if(mid>most)
     81         {
     82             most=mid;
     83             spot=i+1;
     84         }
     85     }
     86  
     87     int cnt=0;
     88     printf("%d
    ",most);
     89  
     90     for(i=0; i<=t; i++)
     91     {
     92         if((con[i]>=65&&con[i]<=90)||(con[i]>=97&&con[i]<=122))
     93         {
     94             cnt++;
     95         }
     96         if(cnt==spot)
     97         {
     98             cnt=0;
     99             for(j=i; cnt<most; j++)
    100             {
    101                 if((con[j]>=65&&con[j]<=90)||(con[j]>=97&&con[j]<=122))
    102                     cnt++;
    103                 printf("%c",con[j]);
    104             }
    105             break;
    106         }
    107     }
    108     if(most!=0)
    109         putchar('
    ');
    110     return 0;
    111 }
    View Code
  • 相关阅读:
    windows环境下面批量修改文件夹名称
    项目中的坑(二)
    微信公众号支付之退款
    微信公众号支付之付款
    excel两张表数据匹配数据(VLOOKUP)
    windows环境下面批量移动文件到指定文件夹里面
    windows环境下面批量新建文件夹
    linux 完全卸载软件方法
    如何获取应用宝APP ID
    mysql 时间函数转换
  • 原文地址:https://www.cnblogs.com/daybreaking/p/9694998.html
Copyright © 2011-2022 走看看