Use the "back-to-text algorithm" to review the C ++ language ., Text Algorithm

Source: Internet
Author: User

Use the "back-to-text algorithm" to review the C ++ language ., Text Algorithm

1. What is text return?

Given a string, read from the back and from the back without changing the string sequence. For example, the customer service phone number of Hebei rural credit cooperatives is "96369". Whether it is read from the back to the back or back to the back, the positions of each character remain unchanged.

Ii. function implementation

(1) give a string and determine whether it is a return text.

(2) given an arbitrary string, determine whether it can be converted back to text. If it can be converted back to text, a specific algorithm is provided.

III. C ++ language implementation version (subsequent implementation of the JAVA language version)

(1) header file (BackText. h)

/*
* BackText. h
*
* Created on: July 15, September 30, 2016
* 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 _*/

(2) Implementation of Classes

/*
* BackText. cpp
*
* Created on: July 15, September 30, 2016
* 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 <"this string cannot be converted into a back-to-text" <endl;
Return;
}
Cout <"Start conversion..." <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;
}
}
}

(3) main Function

/*
* Main. cpp
*
* Created on: July 15, September 30, 2016
* Author: gaodianhua
*/
# Include <iostream>
# Include "BackText. h"
# Include <cstdlib>
# Include <string>
Using namespace std;
Int main (){
String text;
Text. clear ();
Cout <"enter a string :";
Cin> text;
BackText bt = BackText (text );
Bt. print ();
If (! Bt. isBackText ())
Cout <"not a return string" <endl;
Bt. countDiffCh ();
Char dest [100];
Memset (dest, 0x0, sizeof (dest ));
Bt. convert (dest );
Cout <dest <endl;
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.