一、什么是回文字

给定一个字符串,从前往后读和从后往前读,字符串序列不变。例如,河北省农村信用社的客服电话是“96369”,无论从后往前读,还是从前后往后读,各个字符出现的位置不变。

二、功能实现

(一)、给定一个字符串,判断该字符串是否是回文字。

(二)、给定一个任意字符串,判断是否可以转换为回文字,如果可以转换为回文字,给出具体的算法。

三、C++语言实现版本(JAVA语言版本后续实现)

(一)头文件 (BackText.h)

/*
* BackText.h
*
* Created on: 2016年9月30日
* Author: gaodianhua
*/
#include <string>
#include <cstring>
#include <map>
#ifndef BACKTEXT_H_
#define BACKTEXT_H_
using namespace std;

class BackText {
  string text;
  map<char,int> mapBychar;
  int checksum;
  public:
  BackText();
  BackText(char str[]);
  BackText(string text);
  virtual ~BackText();
  bool isBackText();
  void print() const;
  void countDiffCh();
  void convert(char * dest);

};

#endif /* BACKTEXT_H_ */

(二)类的实现

/*
* BackText.cpp
*
* Created on: 2016年9月30日
* Author: gaodianhua
*/

#include "BackText.h"
#include <iostream>
#include <string>
#include <iterator>
#include <cstring>
#include <cstdlib>
#include <map>

using namespace std;

BackText::BackText() {
}

BackText::~BackText() {
  this->checksum=0;
}

BackText::BackText(char *str){
  this->text=str;
  this->checksum=0;
}

BackText::BackText(string str){
  this->text=str;
  this->checksum=0;
}

bool BackText::isBackText(){
  string::iterator it1,it2;
  it1=text.begin();

  it2=text.end()-1;
  for(;it1<=it2;it1++,it2--){
    if(*it1!=*it2)
    return false;
  }
  return true;
}

void BackText::print() const{
  cout<<this->text<<endl;
}

void BackText::countDiffCh(){
  string::iterator it1,it2;
  string temp;
  temp.clear();
  int index=0;
  for(it1=text.begin();it1<text.end();it1++){
    if( strchr(temp.data(),*it1)==NULL ){
      temp.insert(index,1,*it1);
      index++;
    }
  }
  for( it2=temp.begin();it2<temp.end();it2++){
    int count=0;
    for(it1=text.begin();it1<text.end();it1++){
      if(*it1==*it2){
        count++;
        checksum++;
      }
    }
    mapBychar.insert(pair<char,int>(*it2,count));
  }

  map<char,int>::iterator m;
  for(m=mapBychar.begin( );m != mapBychar.end( ); m++ )
    cout <<m->first<<" "<<m->second<<endl;
}

void BackText::convert(char* dest){
  if(isBackText()){
    strcpy(dest,text.data());
    return;
  }
  int cnt=0;
  map<char,int>::iterator m;
  for(m=mapBychar.begin( );m != mapBychar.end( ); m++ ){
    if(m->second%2!=0){
      cnt++;
    }
  }
  if(cnt>1){
    cout<<"该字符串不能被转化为回文字"<<endl;
    return;
  }
  cout<<"开始转换..."<<endl;
  int begIndex=0;
  int endIndex=checksum-1;
  bool oddflag=0;
  char oddchar;
  for(m=mapBychar.begin( );m != mapBychar.end( ); m++ ){
    if( checksum % 2 == 0 ){
      for( int i=0; i< m->second/2; i++ ){
        dest[begIndex++]=m->first;
        dest[endIndex--]=m->first;
      }
    }else{
      if(m->second % 2 == 0){
        for( int i=0; i< m->second/2 ; i++ ){
          dest[begIndex++]=m->first;
          dest[endIndex--]=m->first;
        }
      }else{
        oddchar=m->first;
        oddflag=true;
        continue;
      }
    }
  }
  if(oddflag){
    map<char,int>::iterator it;
    it=mapBychar.find(oddchar);
    if(it==mapBychar.end()){
      cout<<"do not find "<< oddchar <<endl;
      return;
    }
    for( int i=0; i< it->second; i++ ){
      dest[begIndex++]=it->first;
    }
  }
}

(三)main函数

/*
* main.cpp
*
* Created on: 2016年9月30日
* Author: gaodianhua
*/
#include <iostream>
#include "BackText.h"
#include <cstdlib>
#include <string>
using namespace std;
int main(){
  string text;
  text.clear();
  cout<<"请输入字符串:";
  cin>>text;
  BackText bt=BackText(text);
  bt.print();
  if( !bt.isBackText() )
  cout<<"不是回文字符串"<<endl;
  bt.countDiffCh();
  char dest[100];
  memset(dest,0x0,sizeof(dest));
  bt.convert(dest);
  cout<<dest<<endl;
  return 0;
}

通过“回文字算法”复习C++语言。的更多相关文章

  1. 回文字算法(java版本)

    package com.gdh.backtext;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry; p ...

  2. POJ 3974 最长回文字串(manacher算法)

    题意:给出一个字符串,求出最长回文字串. 思路:一开始我直接上了后缀数组DC3的解法,然后MLE了.看了DISCUSS发现还有一种计算回文字串更加优越的算法,就是manacher算法.就去学习了一下, ...

  3. 最长回文字串——manacher算法

    时间复杂度:O(n) 参考:https://segmentfault.com/a/1190000003914228 1.问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字 ...

  4. 求字符串的最长回文字串 O(n)

    昨天参加了某公司的校园招聘的笔试题,做得惨不忍睹,其中就有这么一道算法设计题:求一个字符串的最长回文字串.我在ACM校队选拔赛上遇到过这道题,当时用的后缀数组AC的,但是模板忘了没写出代码来. 回头我 ...

  5. hihocoder 第一周 最长回文字串

    题目1 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程 ...

  6. 2238"回文字串"报告

    题目描述: 回文串,就是从前往后和从后往前看都是一样的字符串.那么现在给你一个字符串,请你找出该字符串中,长度最大的一个回文子串. 输入描述: 有且仅有一个仅包含小写字母的字符串,保证其长度不超过50 ...

  7. 排序算法总结(C语言版)

    排序算法总结(C语言版) 1.    插入排序 1.1     直接插入排序 1.2     Shell排序 2.    交换排序 2.1     冒泡排序 2.2     快速排序 3.    选择 ...

  8. 魔方阵算法及C语言实现

    1 魔方阵概念 填充的,每一行.每一列.对角线之和均相等的方阵,阶数n = 3,4,5….魔方阵也称为幻方阵. 例如三阶魔方阵为: 魔方阵有什么的规律呢? 魔方阵分为奇幻方和偶幻方.而偶幻方又分为是4 ...

  9. 一个UUID生成算法的C语言实现 --- WIN32版本 .

    一个UUID生成算法的C语言实现——WIN32版本   cheungmine 2007-9-16   根据定义,UUID(Universally Unique IDentifier,也称GUID)在时 ...

随机推荐

  1. javascript for..in 知识

        JavaScript 中for...in...,一直在代码中使用,最初认为,for xx in obj||array 对于arry就xx对应为数组索引,对于obj来说xx对应就是obj中的pr ...

  2. IIS集成模式下,URL重写后获取不到Session值

    近期给公司网站添加了伪静态功能,但是今天发现了在伪静态的页面中,Session值是获取不到的. 原因是在伪静态请求的时候,Session请求被“过滤”掉了. 开始是把web.config文件中的mod ...

  3. 【原创】NIO框架入门(一):服务端基于Netty4的UDP双向通信Demo演示

    申明:本文由作者基于日常实践整理,希望对初次接触MINA.Netty的人有所启发.如需与作者交流,见文签名,互相学习. 学习交流 更多学习资料:点此进入 推荐 移动端即时通讯交流: 215891622 ...

  4. 快速入门系列--Log4net日志组件

    Log4net是阿帕奇基金会的非常流行的开源日志组件,是log4j的.NET移植版本,至今已经有11年的历史,使用方便并且非常稳定,此外很重要的一点是其和很多开源组件能很好的组合在一起工作,例如NHi ...

  5. mongodb 关系、引用、覆盖索引查询

    一.关系 MongoDB 的关系表示多个文档之间在逻辑上的相互联系.文档间可以通过嵌入和引用来建立联系.MongoDB 中的关系可以是:1对1,1对多,多对1,多对多. 一个用户可以用多个地址,这是典 ...

  6. SQL 语句中union all和order by同时使用

            最近做的一个财物管理系统中查询过期或逾期的存储过程,返回 “财物所属的案件名称”,“财物名称”,“财物编号”,“过期或逾期时间”(超期或逾期前7天开始预警). 遇到“union all ...

  7. Foundation 5 发布!最先进的响应式前端框架

    数以百万计的设计师和工程师采用 Foundation 作为他们的产品和网站设计的前端框架.Foundation 是第一个响应式.语义化和移动优先的开源框架.最新发布的 Foundation 5 是最先 ...

  8. 在Spring Boot中使用Https

    本文介绍如何在Spring Boot中,使用Https提供服务,并将Http请求自动重定向到Https. Https证书 巧妇难为无米之炊,开始的开始,要先取得Https证书.你可以向证书机构申请证书 ...

  9. 【Java基础】类和接口

    Num1:使类和成员的可访问性最小化 要区别设计良好的模块与设计不好的模块,最重要的因素在于,这个模块对于外部的其他模块而言,是否隐藏其内部数据和其他实现细节.设计良好的模块会隐藏所有的实现细节,把它 ...

  10. Azure ARM (11) ARM模式下,创建虚拟机并配置负载均衡器

    <Windows Azure Platform 系列文章目录> 本文内容比较多,请大家仔细阅读,谢谢! 在前几章中,我们做了准备工作: 1.创建ARM Resouce Group,叫Lei ...