通过“回文字算法”复习C++语言。
一、什么是回文字
给定一个字符串,从前往后读和从后往前读,字符串序列不变。例如,河北省农村信用社的客服电话是“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++语言。的更多相关文章
- 回文字算法(java版本)
package com.gdh.backtext;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry; p ...
- POJ 3974 最长回文字串(manacher算法)
题意:给出一个字符串,求出最长回文字串. 思路:一开始我直接上了后缀数组DC3的解法,然后MLE了.看了DISCUSS发现还有一种计算回文字串更加优越的算法,就是manacher算法.就去学习了一下, ...
- 最长回文字串——manacher算法
时间复杂度:O(n) 参考:https://segmentfault.com/a/1190000003914228 1.问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字 ...
- 求字符串的最长回文字串 O(n)
昨天参加了某公司的校园招聘的笔试题,做得惨不忍睹,其中就有这么一道算法设计题:求一个字符串的最长回文字串.我在ACM校队选拔赛上遇到过这道题,当时用的后缀数组AC的,但是模板忘了没写出代码来. 回头我 ...
- hihocoder 第一周 最长回文字串
题目1 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程 ...
- 2238"回文字串"报告
题目描述: 回文串,就是从前往后和从后往前看都是一样的字符串.那么现在给你一个字符串,请你找出该字符串中,长度最大的一个回文子串. 输入描述: 有且仅有一个仅包含小写字母的字符串,保证其长度不超过50 ...
- 排序算法总结(C语言版)
排序算法总结(C语言版) 1. 插入排序 1.1 直接插入排序 1.2 Shell排序 2. 交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择 ...
- 魔方阵算法及C语言实现
1 魔方阵概念 填充的,每一行.每一列.对角线之和均相等的方阵,阶数n = 3,4,5….魔方阵也称为幻方阵. 例如三阶魔方阵为: 魔方阵有什么的规律呢? 魔方阵分为奇幻方和偶幻方.而偶幻方又分为是4 ...
- 一个UUID生成算法的C语言实现 --- WIN32版本 .
一个UUID生成算法的C语言实现——WIN32版本 cheungmine 2007-9-16 根据定义,UUID(Universally Unique IDentifier,也称GUID)在时 ...
随机推荐
- qqzoneQQ空间漏洞扫描器的设计attilax总结
qqzoneQQ空间漏洞扫描器的设计attilax总结 1.1. 获取对方qq(第三方,以及其他机制)1 1.2. QQ空间的html流程1 1.3. 判断是否有权限1 1.4. 2015年度Web服 ...
- redis常用操作总结
在项目中时常会用到redis,redis看起来好像很难的样子,而且我也确认反复学习了很久,但是,总结下来,自己使用到的东西并不太多,如下作一些总结工作. 1.安装(单机) 1.1 windows, 直 ...
- salesforce 零基础学习(三十三)通过REST方式访问外部数据以及JAVA通过rest方式访问salesforce
本篇参考Trail教程: https://developer.salesforce.com/trailhead/force_com_dev_intermediate/apex_integration_ ...
- 快速入门系列--MySQL
一直说要好好复习一下Mysql都木有时间,终于赶上最近新购买了阿里云,决定使用CentOS去试试.NET Core等相关的开发,于是决定好好的回顾下这部分知识,由于Mysql的数据库引擎是插件式的,对 ...
- java生成excel文件
首先下载jexcelapi_2_6_12.tar.gz,解压后将里面的jxl.jar复制到WEB-INF/lib目录下面 String filePath = request.getParameter( ...
- Flume官方文档翻译——Flume 1.7.0 User Guide (unreleased version)(一)
Flume 1.7.0 User Guide Introduction(简介) Overview(综述) System Requirements(系统需求) Architecture(架构) Data ...
- 可视化工具solo show-----Prefuse自带例子GraphView讲解
2014.10.15日以来的一个月,挤破了头.跑断了腿.伤透了心.吃够了全国最大餐饮连锁店——沙县小吃.其中酸甜苦辣,绝不是三言两语能够说得清道的明的.校招的兄弟姐妹们,你们懂得…… 体会最深的一句话 ...
- JAVA之IO文件读写
IO概述: IO(Input output)流 作用:IO流用来处理设备之间的数据传输 ...
- JS&CSS文件请求合并及压缩处理研究(四)
本篇将会尝试对之前的代码进行相关的单元测试,验证路径合并规则的覆盖率及正确性. 熟悉 ASP.NET MVC 开发的朋友应该知道,微软在MVC框架下集成了一款名为 Microsoft.VisualSt ...
- qml基础学习 模型视图(一)
一.理解qml模型和视图 qt的发展是迅速的,虽然在每一个release版本中或多或少都有bug,但是作为一个庞大的gui库,no,应该说是一个开发框架开说,qt已经算是做的相当好.qml部分是qt4 ...