全RE!求调!

B4039 [GESP202409 三级] 回文拼接

turing_hsy @ 2024-11-26 21:04:34

#include<bits/stdc++.h>
using namespace std;
bool check(string a)
{
    int j=0;
    for(int i=a.size()-1;i>=0;i--)
    {
        if(a[j]!=a[i])
        {
            return 0;
        }
        j++;
    }
    return 1;
}
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string a;
        cin>>a;
        bool s=0;
        for(int i=1;i<a.size()-2;i++)
        {

            string s1="",s2="";
            for(int j=0;j<=i;j++) s1+=a[j];
            for(int j=i+1;j<a.size();j++) s2+=a[j];
            if(check(s1)==1&&check(s2)==1) 
            {
                cout<<"Yes"<<endl;
                s=1;
                break;
            }

        }
        if(s==0)cout<<"No"<<endl;
    } 
    return 0;
}

by 1211920dfc @ 2024-11-28 17:34:17

#include<bits/stdc++.h>
using namespace std;
bool check(string a)
{
    int j=0;
    for(int i=a.size()-1;i>=0;i--)
    {
        if(a[j]!=a[i])
        {
            return 0;
        }
        j++;
    }
    return 1;
}
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string a;
        cin>>a;
        bool s=0;
        if (a.size()>2)//this
        {
            for(int i=1;i<a.size()-2;i++)
            {

                string s1="",s2="";
                for(int j=0;j<=i;j++) s1+=a[j];
                for(int j=i+1;j<a.size();j++) s2+=a[j];
                if(check(s1)==1&&check(s2)==1) 
                {
                    cout<<"Yes"<<endl;
                    s=1;
                    break;
                }

            }
        }

        if(s==0)cout<<"No"<<endl;
    } 
    return 0;
}

by 1211920dfc @ 2024-11-28 17:35:17

@turing_hsy


by turing_hsy @ 2024-11-28 17:59:15

@1211920dfc
谢谢大佬
膜拜


by turing_hsy @ 2024-11-28 18:03:23

@1211920dfc
https://www.luogu.com.cn/record/191652291
AC代码

感谢大佬


by turing_hsy @ 2024-11-28 18:06:39

AC链接
不是AC代码,发错了不好意思


by zhx0919 @ 2024-12-06 00:14:05

@1211920dfc if (a.size()>2)//this else的情况就是长度为1,2的情况,那么在for的条件中:for(int i=1;i<a.size()-2;i++),也不会成立的,for是不会执行的,所以这里增加了if(a.size()>2)有何意义?


by zhx0919 @ 2024-12-06 00:28:05

@1211920dfc 原因是:s.size()返回的是一个unsigned int,当一个unsigned int 和一个int比较时,会把int转换成unsigned,-1转换成unsigned就变成了0xffff……,无穷大了,也就满足for的条件了,这里只要将s.size()返回值强制转换成int 就可以了


by turing_hsy @ 2024-12-22 19:37:38

@zhx0919
感谢大佬


|