20分,大佬求改!!!

B4039 [GESP202409 三级] 回文拼接

feng_chenShy @ 2024-11-17 23:12:39

#include <iostream>
#define in scanf
#define out printf
using namespace std;
long long T;
string s;
int main(){
    in ("%lld" , &T);
    while (T --){
        cin >> s;
        int l = s.size();
        int a1 , b1 , a2 , b2;//表示两个字符串的头和尾
        bool f1 = false , f2 = false;//两个字符串的状态
        a1 = 0;
        for (int i = 1;i < l - 2;i ++){//判断第一个回文字符串的头和尾
            if (s[i] == s[a1]){
                b1 = i;
                f1 = true;
                if (b1 - a1 + 1 == 2){//如果是一个长度为2的回文,直接退出找的过程
                    break;
                }
                for (int k = 0;k <= (b1 - 1) / 2;k ++){//判断是否为回文
                    if (s[k] != s[b1 - k]){
                        f1 = false;
                        b1 = NULL;
                        break;
                    }
                }
                if (f1) break;
            }
        }
        if (!f1){//如果第一个不是,输出No
            out ("No\n");
            continue;
        }
        a2 = b1 + 1;//判断第二个字符转
        if (s[a2] == s[l - 1]){
            b2 = l - 1;
            if (b2 - b1 + 1 >= 2){//判断长度
                f2 = true;
                for (int k = a2;k <= (l - 2) / 2;k ++){//判断是否为回文
                    if (s[k] != s[l - 1 - k]){
                        f2 = false;
                        b2 = NULL;
                        break;
                    }
            }   
            }
        }
        if (f1 && f2)   out ("Yes\n");
        else    out ("No\n");
    }
    return 0;
}

by Diary_Of_Young @ 2024-11-17 23:25:34

它的上级是最长双回文串。虽然可以马拉车,但是最稳的还是暴力出奇迹


by Diary_Of_Young @ 2024-11-17 23:29:39

bool check(string s) {
    string y = s;
    reverse(y.begin(), y.end());
    return s == y;
}

by feng_chenShy @ 2024-11-24 15:36:59

@Diary_Of_Young谢谢大佬,会了!


|