Character elimination, just a simple review of the container in C + +. Containers are easier to use, including string types, code or write often, before you know where you are.
#include"StdAfx.h"#include <iostream> #include <string> #include <Vector>usingnamespaceStdstringStringremove (strings) {stringTemp Char Temp_c; for(inti =0; I < S.size(); i++) {temp_c = s[i]; Temp.push_back (Temp_c);if(S[i +1] = = Temp_c) {temp.pop_back (); } while(S[i +1] = = Temp_c) {i++; } }if(temp.size() = = S.size()) {returnTemp }Else{temp = Stringremove (temp); }returnTemp//Recursive forget this sentence}intMain () {stringSstringSout//character string after elimination intN//number of input data intCount =0;//number of elimination countsCIN >> N;intLen//Record the length of the elimination result int min;//Record elimination results in the shortest intPosition//Record elimination results in the shortestChar Whichone;//Location of record elimination intScore;//Record elimination score intI, J; while(n--) {CIN >> S;min= S.size();//cout << "Output" << Endl; string:: Iterator begin = S.begin ();string:: Iterator insert;stringS_copy; for(i =0; I <= S.size(); i++) { for(j = $; J < the; J + +) {insert = S.insert (begin, J);//cout << s << endl;S_copy = s; Sout = Stringremove (s_copy);//cout << "string remaining length:" << sout.size () << Endl;Len = sout.size();if(Len <=min) {min= Len; Position = i; Whichone = j; Score = S.size()-Sout.size(); } s.erase (insert); }if(I! = S.)size()) {begin++; }} cout << score << Endl;/*cout << "Location:" <<position << "<< whichone<<" score: "<<score << endl;*/ }return 0;}
The first set of data: "ABCBCCCAA" in the 2nd word specifier insert ' C ' to get "ABCCBCCCAA", eliminate the "A", a total elimination of 9 characters (including the inserted ' C ').
The second set of data: "AAA" Insert ' A ' to get "AAAA", after elimination to get "", total elimination of 4 characters.
The third set of data: Whether you get "AABC" after inserting a character, "ABBC" or "ABCC" will eliminate up to 2 characters.
Sample input
3
Abcbcccaa
Aaa
Abc
Sample output
9
4
2
Welcome to the Csdn-markdown Editor