全TLE救救

B4039 [GESP202409 三级] 回文拼接

huangtianle666 @ 2024-12-11 13:13:11

#include<bits/stdc++.h>
using namespace std;
string a,ia,ib;
bool flag;
int n;
bool f(string a){
    string b="";
    for(int i=0;i<a.size();i++){
        b=a[i]+b;
    }
    return a==b;
}
int main(){
    scanf("%d",&n);
    while(n--){
        flag=false;
        cin>>a;
        for(int i=2;i<a.size()-2;i++){
            ia=ib="";
            for(int j=0;j<i;j++){
                ia=ia+a[j];
            }
            for(int j=i;j<a.size();j++){
                ib=ib+a[j];
            }
            if(f(ia)&&f(ib)){
                printf("Yes\n");
                flag=true;
                break;
            }
        }
        if(!flag){
            printf("No\n");
        }
    }
    return 0;
}

by lsrlsrlsr @ 2024-12-15 11:05:58

#include<bits/stdc++.h>
using namespace std;
string a,ia,ib;
bool flag;
int n;
bool f(string a){
    string b="";
    for(int i=0;i<a.size();i++){
        b=a[i]+b;
    }
    return a==b;
}
int main(){
    scanf("%d",&n);
    while(n--){
        flag=false;
        cin>>a;
        for(int i=2;i<a.size();i++){
            ia=ib="";
            ia=a.substr(0,i);
            ib=a.substr(i,a.size());
            if(ia.size()<2||ib.size()<2){
                continue;
            }
            if(f(ia)&&f(ib)){
                printf("Yes\n");
                flag=true;
                break;
            }
        }
        if(!flag){
            printf("No\n");
        }
    }
    return 0;
}

by lsrlsrlsr @ 2024-12-15 11:06:52

还在吗? 求关


by lsrlsrlsr @ 2024-12-15 11:09:15

for循环效率太低,用C++STL里的函数

字符串名.substr(开始位置,结束位置后一个)


by lsrlsrlsr @ 2024-12-15 11:15:38

@huangtianle666


by huangtianle666 @ 2024-12-16 12:50:53

@lsrlsrlsr谢谢,已关


|