题解 | #统计回文#

统计回文

https://www.nowcoder.com/practice/9d1559511b3849deaa71b576fa7009dc

统计回文

统计回文

/*
2022年09月10日 11:35:10
遍历str1的位置,挨个插入str2,并判断是否为回文串
注意拷贝一份str1,而不能直接修改str1
*/


#include <iostream>
#include <string>
using namespace std;

bool IsPalindrome(string& s){
    int begin = 0, end = s.size()-1;
    while(begin < end){
        if(s[begin] != s[end])
            return false;
        ++begin, --end;
    }
    return true;
}

int main()
{
    string str1, str2;
    cin >> str1 >> str2;
    // str1的最后也要能够插入
    int count = 0;
    for(int i = 0; i <= str1.size(); ++i)
    {
        string tmp(str1);
        tmp.insert(i, str2);
        if(IsPalindrome(tmp))
            ++count;
    }
    cout << count << endl;
    return 0;
}
/*
2022年09月10日 11:40:42
遍历str1的位置,挨个插入str2,并判断是否为回文串
注意拷贝一份str1,而不能直接修改str1

判断回文也可以翻转之后 直接比较字符串是否相等
*/


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool IsPalindrome(string& s){
    string tmp = s;
    reverse(s.begin(), s.end());
    if(s == tmp)
        return true;
    else
        return false;
}

int main()
{
    string str1, str2;
    cin >> str1 >> str2;
    // str1的最后也要能够插入
    int count = 0;
    for(int i = 0; i <= str1.size(); ++i)
    {
        string tmp(str1);
        tmp.insert(i, str2);
        if(IsPalindrome(tmp))
            ++count;
    }
    cout << count << endl;
    return 0;
}
全部评论

相关推荐

09-19 12:15
门头沟学院 Java
迷茫的大四🐶:这下是真的打牌了,我可以用感谢信和佬一起打牌吗
点赞 评论 收藏
分享
10-09 17:17
已编辑
门头沟学院 Java
活泼的代码渣渣在泡池...:同学你好,我也是学院本,后天要面这个亚信科技,是实习,请问问题都啥样呀,我项目就做了网上的,这是第一次面试
投递多益网络等公司10个岗位
点赞 评论 收藏
分享
27双非本,最近面试被挂麻了面试官说简历内容太简单了,技术栈要单独一行,各位佬有啥建议吗
LZStarV:项目太简单了,你像用什么开发的技术栈没必要写一句话,按点写就好了;有特色的比如说WebSocket、视频流这种狠狠吹,那就好看多了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务