1040. Longest Symmetric String (25)

102 篇文章 0 订阅
26 篇文章 0 订阅

1040. Longest Symmetric String (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
发现以前做过了;
求最长的回文串长度;就是吧所有的可能都求一下,得出最长,这里利用了“/”取模特性,把奇数和偶数的情况一起讨论了
联动以前做的友元函数模版或者看后面贴的
http://xujiayu317.blog.163.com/blog/static/2547520920144254155345/

评测结果

时间 结果 得分 题目 语言 用时(ms) 内存(kB) 用户
8月05日 13:45 答案正确 25 1040 C++ (g++ 4.7.2) 2 384 datrilla

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 1 308 13/13
1 答案正确 1 308 2/2
2 答案正确 1 248 2/2
3 答案正确 2 180 2/2
4 答案正确 1 300 2/2
5 答案正确 1 308 2/2
6 答案正确 1 384 2/2
#include<iostream> 
#include<string>
using namespace std; 
int  longest(string str)
{
  int len = str.size();
  int nowlen, right, left, counts;
    for (counts = nowlen = 0; str[nowlen/2]; nowlen++)
    {
      right = (nowlen+1) / 2;
      left = nowlen / 2;/*当nowlen为奇数时,right和left一样,当nowlen为偶数时,两者不一样,nowlen范围0~2*len*/
      while (right < len&&left >= 0 && str[right] == str[left])
      {
        right++;
        left--;
      }
      if (counts < right - left - 1)
        counts = right - left - 1;
    }
    return counts;
}
int main()
{
  string str;
  getline(cin, str);
  cout << longest(str) << endl;
  system("pause"); 
  return 0;
}

评测结果

时间 结果 得分 题目 语言 用时(ms) 内存(kB) 用户
8月05日 13:21 答案正确 25 1040 C++ (g++ 4.7.2) 2 308 datrilla

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 1 180 13/13
1 答案正确 1 256 2/2
2 答案正确 1 308 2/2
3 答案正确 2 308 2/2
4 答案正确 1 256 2/2
5 答案正确 1 308 2/2
6 答案正确 1 308 2/2
#include<iostream>
using namespace std;
class  MyString
{
  friend void longest(MyString &);
private:

   char str[1000];
   int  count,i,add,dec;
};

void longest(MyString &b)
{
  
  while(gets(b.str)&&b.str!=NULL)
  {
    b.count=0;
    b.i=0;
    while(b.str[b.i/2])
    {
           b.add=(b.i+1)/2;
       b.dec=b.i/2;
       while(b.dec>-1&&b.add<1000&&b.str[b.dec]&&b.str[b.add]
         &&b.str[b.dec]==b.str[b.add])
       {
                      b.dec--;b.add++;
       }
          if(b.count<b.add-b.dec-1)b.count=b.add-b.dec-1;
         b.i++;
    }
    cout<<b.count<<endl;
  }
}
int main()
{
  MyString MS;
  longest(MS);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值