首页 > 代码库 > PAT 1040 Longest Symmetric String
PAT 1040 Longest Symmetric String
#include <cstdio>#include <cstdlib>using namespace std;char line[1001];char line2[2003];int syslen(char str[], int start) { int len = 1; int p = start - 1; int q = start + 1; while (p >=0 && str[q] != ‘\0‘ && str[p] == str[q]) { p--, q++; len += 2; } return len;}int main() { scanf("%[^\n]s", line); int p = 0, q = 0; line2[p++] = ‘ ‘; line2[p++] = line[q++]; while (line[q] != ‘\0‘) { line2[p++] = ‘ ‘; // dummy char used as seperator line2[p++] = line[q++]; } line2[p++] = ‘ ‘; line2[p] = ‘\0‘; int max_len = 0; for (int i=0; i<p; i++) { int cur_len = syslen(line2, i); if (cur_len > max_len) max_len = cur_len; } printf("%d", max_len/2); return 0;}
brute force for small case
PAT 1040 Longest Symmetric String
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。