通過“回文字算法”復(fù)習(xí)C++語言
一、什么是回文字
給定一個(gè)字符串,從前往后讀和從后往前讀,字符串序列不變。例如,河北省農(nóng)村信用社的客服電話是“96369”,無論從后往前讀,還是從前后往后讀,各個(gè)字符出現(xiàn)的位置不變。
二、功能實(shí)現(xiàn)
(一)、給定一個(gè)字符串,判斷該字符串是否是回文字。
(二)、給定一個(gè)任意字符串,判斷是否可以轉(zhuǎn)換為回文字,如果可以轉(zhuǎn)換為回文字,給出具體的算法。
三、C++語言實(shí)現(xiàn)版本(JAVA語言版本后續(xù)實(shí)現(xiàn))
(一)頭文件 (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_ */
(二)類的實(shí)現(xiàn)
/* * 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<<"該字符串不能被轉(zhuǎn)化為回文字"<<endl; return; } cout<<"開始轉(zhuǎn)換..."<<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函數(shù)
/* * 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<<"請(qǐng)輸入字符串:"; 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; }
以上所述是小編給大家分享的通過“回文字算法”復(fù)習(xí)C++語言,希望對(duì)大家有所幫助!
- C++回文數(shù)及素?cái)?shù)問題計(jì)算方法
- C++實(shí)現(xiàn)判斷字符串是否回文實(shí)例解析
- C++ 數(shù)據(jù)結(jié)構(gòu)鏈表的實(shí)現(xiàn)代碼
- 淺析C++中單鏈表的增、刪、改、減
- C++中單鏈表的建立與基本操作
- C++循環(huán)鏈表之約瑟夫環(huán)的實(shí)現(xiàn)方法
- c++雙向鏈表操作示例(創(chuàng)建雙向鏈、雙向鏈表中查找數(shù)據(jù)、插入數(shù)據(jù)等)
- C++實(shí)現(xiàn)的鏈表類實(shí)例
- 用C++實(shí)現(xiàn)單向循環(huán)鏈表的解決方法
- C++鏈表倒序?qū)崿F(xiàn)方法
- C++數(shù)據(jù)結(jié)構(gòu)與算法之判斷一個(gè)鏈表是否為回文結(jié)構(gòu)的方法
相關(guān)文章
C語言設(shè)置和取得socket狀態(tài)的相關(guān)函數(shù)用法
這篇文章主要介紹了C語言設(shè)置和取得socket狀態(tài)的相關(guān)函數(shù)用法,分別是setsockopt()函數(shù)和getsockopt()函數(shù)的使用介紹,需要的朋友可以參考下2015-09-09解決C++ 無法從void 轉(zhuǎn)換為L(zhǎng)RESULT的方法詳解
本篇文章是對(duì)C++中無法從void轉(zhuǎn)換為L(zhǎng)RESULT的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C++設(shè)計(jì)模式中的工廠模式詳細(xì)介紹
工廠模式,是一種實(shí)例化對(duì)象的方式,只要輸入需要實(shí)例化對(duì)象的名字,就可以通過工廠對(duì)象的相應(yīng)工廠函數(shù)來制造你需要的對(duì)象2022-09-09C語言中多維數(shù)組的內(nèi)存分配和釋放(malloc與free)的方法
寫代碼的時(shí)候會(huì)碰到多維數(shù)組的內(nèi)存分配和釋放問題,在分配和釋放過程中很容易出現(xiàn)錯(cuò)誤。下面貼上一些示例代碼,以供參考。2013-05-05C++實(shí)現(xiàn)LeetCode(131.拆分回文串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(131.拆分回文串),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07