博客
关于我
9.统计回文
阅读量:122 次
发布时间:2019-02-27

本文共 1195 字,大约阅读时间需要 3 分钟。

为了解决这个问题,我们需要找到将字符串B插入字符串A的所有可能位置,使得插入后的字符串是一个回文串。回文串是指正读和反读都一样的字符串。

方法思路

  • 回文判断函数:首先,我们需要一个函数来判断一个字符串是否是回文串。这个函数从字符串的两端同时开始比较字符,直到中间位置。
  • 遍历插入位置:对于每个可能的位置,将字符串B插入到字符串A的位置,然后检查插入后的字符串是否是回文。
  • 处理特殊情况:如果字符串B为空,那么插入后的字符串就是原来的字符串A。如果A是回文,那么方法数是A的长度加1,否则为0。
  • 解决代码

    #include 
    #include
    using namespace std;bool IsCircle(const 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; getline(cin, str1); getline(cin, str2); if (str2.empty()) { if (IsCircle(str1)) { cout << str1.size() + 1 << endl; } else { cout << 0 << endl; } return 0; } int count = 0; for (int i = 0; i <= str1.size(); i++) { string temp = str1; temp.insert(i, str2); if (IsCircle(temp)) { count++; } } cout << count << endl; return 0;}

    代码解释

  • IsCircle函数:这个函数检查字符串是否是回文。从字符串两端开始,逐步向中间比较字符,直到两端相遇。
  • 读取输入:从标准输入读取字符串A和B。
  • 处理空字符串B:如果B为空,检查A是否是回文,如果是,输出A的长度加1,否则输出0。
  • 遍历插入位置:对于每个可能的位置插入B,生成新字符串并检查是否是回文,统计符合条件的情况。
  • 输出结果:输出满足条件的插入方法数。
  • 转载地址:http://jpbb.baihongyu.com/

    你可能感兴趣的文章
    oracle partition by list,深入解析partition-list 分区
    查看>>
    Oracle PL/SQL Dev工具(破解版)被植入勒索病毒的安全预警及自查通告
    查看>>
    oracle rac集群的东西之QQ聊天
    查看>>
    Oracle Schema Objects——Tables——Table Compression
    查看>>
    oracle scott趣事
    查看>>
    oracle script
    查看>>
    Oracle select表要带双引号的原因
    查看>>
    Oracle SOA Suit Adapter
    查看>>
    Oracle Spatial GeoRaster 金字塔栅格存储
    查看>>
    Oracle spatial 周边查询SQL
    查看>>
    Oracle Spatial空间数据库建立
    查看>>
    UML— 活动图
    查看>>
    oracle sqlplus已停止工作,安装完成客户端后sqlplus报“段错误”
    查看>>
    oracle SQLserver 函数
    查看>>
    oracle sql分组(group,根据多个内容分组)在select之后from之前 再进行select查询,复杂子查询的使用
    查看>>
    Oracle Statspack分析报告详解(一)
    查看>>
    oracle tirger_在Oracle中,临时表和全局临时表有什么区别?
    查看>>
    Oracle Validated Configurations 安装使用 说明
    查看>>
    oracle where 条件的执行顺序分析1
    查看>>
    oracle 中的 CONCAT,substring ,MINUS 用法
    查看>>