一、什么是回文字

给定一个字符串,从前往后读和从后往前读,字符串序列不变。例如,河北省农村信用社的客服电话是“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. Atitit  循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate).

    Atitit  循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate). 1.1. 循环算是最基础的概念, 凡是重复执行一段代码, 都可以称之为循环. ...

  2. LLBL Gen + Entity Framework 程序设计入门

    Entity Framework推出有好几年,除了微软的Visual Studio可以做实体框架开发外,第三方的开发工具如LLBL Gen, Devart Entity Developer也可以用来做 ...

  3. BrowserSync前端调试工具使用

    上次介绍了一款DebugGap移动端调试工具DebugGap推荐.但是这几天使用了之后感觉还是有些不足,尤其是里面的调试工具虽然和Chrome里面的调试长的很像,但是多少有些不同,使用起来还是不太方便 ...

  4. 如何配置Hyper-V的虚拟机通过主机网络上网 (NAT)

    前言 最近开始在Windows 8 上面直接使用Hyper-V的技术来建立虚拟环境进行开发和测试,这样免去了再安装额外软件的需要.在实际使用的时候,尤其是配置网络共享的时候,遇到些问题,与其他一些虚拟 ...

  5. 邻接表有向图(三)之 Java详解

    前面分别介绍了邻接表有向图的C和C++实现,本文通过Java实现邻接表有向图. 目录 1. 邻接表有向图的介绍 2. 邻接表有向图的代码说明 3. 邻接表有向图的完整源码 转载请注明出处:http:/ ...

  6. Docker的学习--介绍和安装

    什么是 Docker Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go 语言实现. 项目后来加入了 Lin ...

  7. c#Dictionary键值对的使用

    直接粘代码吧 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  8. Hadoop官方文档翻译—— YARN ResourceManager High Availability 2.7.3

    ResourceManager High Availability (RM高可用) Introduction(简介) Architecture(架构) RM Failover(RM 故障切换) Rec ...

  9. Hadoop阅读笔记(四)——一幅图看透MapReduce机制

    时至今日,已然看到第十章,似乎越是焦躁什么时候能翻完这本圣经的时候也让自己变得更加浮躁,想想后面还有一半的行程没走,我觉得这样“有口无心”的学习方式是不奏效的,或者是收效甚微的.如果有幸能有大牛路过, ...

  10. redis学习之二from github

    大概敲了一遍基本命令,熟悉了redis的存储方式.现在开始进一步系统的学习.学习教程目前计划有三个,一个是github上的https://github.com/JasonLai256/the-litt ...