您的位置:首页 > 其它

1040. Longest Symmetric String (25)

2015-08-05 13:54 323 查看


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答案正确251040C++
(g++ 4.7.2)
2384datrilla


测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确130813/13
1答案正确13082/2
2答案正确12482/2
3答案正确21802/2
4答案正确13002/2
5答案正确13082/2
6答案正确13842/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答案正确251040C++
(g++ 4.7.2)
2308datrilla


测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确118013/13
1答案正确12562/2
2答案正确13082/2
3答案正确23082/2
4答案正确12562/2
5答案正确13082/2
6答案正确13082/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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: