Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|
26827 | 唐心 | 统计回文子串 | C++ | Accepted | 119 MS | 744 KB | 1011 | 2022-05-24 17:06:30 |
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N = 5001; char str[N]; int expand_center(char s[], int left, int right) { int num = 0; int len = strlen(s); // cout<<left<<" "<<right<<endl; // 中心拓展法,从中间往两边不断拓展 while (left >= 0 && right < len) { if(s[left] == s[right]) { num++; } else { break; } --left; ++right; } // cout<<num<<endl; // 最后走到的最长回文串长度,夹在left和right中间 return num; } int main () { int cnt1 = 0,cnt2 = 0; int len,i; while ( scanf("%s", str) != EOF ) { cnt1 = 0; cnt2 = 0; len = strlen(str); for(i = 0;i<len;i++) { // 以第i个字符为中心向两边拓展,奇数 cnt1 += expand_center(str, i, i); // 以第i个字符和i+1个字符的两端向两边拓展,偶数 cnt2 += expand_center(str, i, i + 1); } cout<<cnt1+cnt2<<endl; } return 0; }