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 | 答案正确 | 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; }
相关文章推荐
- CentOS 6.5 升级内核到 3.10.28
- 代码整洁之道--类
- java.lang.OutOfMemoryError处理错误
- .net反混淆脱壳工具de4dot的使用
- 破解NET的四大神器(转)
- C++为了兼容,所以并不是纯面向对象编程语言
- 谷歌浏览器修改CSS和js后同步保存到文件中 (译)
- 南邮 OJ 1230 最小m段和问题
- POJ Mayor's posters 线段树
- The Architecture of Open Source Applications-知名开源项目各种架构分析-前言
- Gomoku
- GetWindowLong() 和 SetWindowLong()
- 写C# dll供Unity调用
- vs 快捷键盘
- queue
- 南邮 OJ 1228 租用游艇问题
- 初探使用iOS 7 Sprite Kit与Cocos2d开发游戏的对比
- OpenCV2:特征匹配及其优化
- jquery 中的post和get方法同步问题 解决办法
- 字符串反转