输出字符串中对称的子字符串的最大长度。
输入描述:
测试输入包含1个测试用例,一个字符串str。
输出描述:
输出最长的子字符串长度。
输入例子:
roorle
输出例子:
4
链接:https://mp.weixin.qq.com/s?__biz=MzI1MTIzMzI2MA==&mid=2650560782&idx=2&sn=e15ea73c2fcbf62f050b1ef9ae7d0a1f&chksm=f1feef8dc689669b232c09ab799d4c4972baf08858f112427b2d14cb5471e76aeed6164282ce&scene=0#rd
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> int MaxLength( char* s ) { int ans = 1; int len = std::strlen(s); int Record[len]; Record[0] = 1; for (int i = 1; i < len ; ++i) { int max = 1; if ( (i-Record[i-1]-1) >= 0 && s[i]==s[i-Record[i-1]-1]) { max = std::max(max,( Record[i-1]+2)); } int k = 1; while( s[i] == s[i-k] ) { k++; } max = std::max( max, k ); Record[i] = max; ans = std::max( max , ans ); } return ans; } char s[100010]; int main() { //std::cout << "Hello, World!" << std::endl; freopen("F:\\CSLeaning\\Thinking in C++\\MaxLengh\\in.in" , "r", stdin); memset(s, 0, sizeof(s)); while( std::cin>>s ) { std::cout<<MaxLength(s)<<std::endl; memset(s, 0, sizeof(s)); } return 0; }

被折叠的 条评论
为什么被折叠?



