C + + Solve string (minimum substring, maximum substring, delete specified string)

Source: Internet
Author: User
Tags first string

#include <iostream> #include <string.h> #define SIZE 10#define MAXVALUE 0x7fffffffusing namespace std;//the topic is: To find the smallest string in a literal.//To find the minimum string, more difficult than the maximum string, the following there is my maximum string code, I did not think of a better method, the following method, although the space complexity is too large, but the complexity of time is relatively fast. Template<typename t>struct node{t data;//data int index;//save subscript. };template<typename t>class mynode{public:mynode (t *a=null,int sz = size,t a1=t (), T a2=t ())//Start my structure, will contain A1, All locations of the A2 are stored inside the node. {node = new node<t>[sz];size=0;for (int i=0;i<sz;i++) {if (A[I]==A1) {node[size].data = A1;node[size].index = i; size++;} if (a[i]==a2) {node[size].data = A2;node[size].index = i;size++;}}} int mindex (int &x,int &y)//Gets the subscript x, Y for the most small string. {int n = size-1;int min=maxvalue;for (int i=n;i>0;i--) {for (int j=i-1;j>=0;j--) {if (node[i].data!=node[j].data) { if ((Node[i].index-node[j].index) <min) {min = Node[i].index-node[j].index;y = Node[i].index;x = Node[j].index;}}} return 0;} char *getstrmin (int x,int y,char *str)//followed by subscript to get the smallest string. {Char *p = str;p + = x;* (str+y+1) = ' + '; return p;} Private:int size; Node<t> *node;}; int main () {char a[]= "CBBBBBBAAAAABBBC"; Mynode<char> Mynode (A,strlen (a), ' a ', ' C ');//The Order of AC here is not considered. int X,y;mynode. Mindex (x, y); Cout<<mynode. Getstrmin (X,y,a) <<endl;return 0;} ----------------------------------------------------------#include <iostream> #include <string.h>// This is the solution of the maximum string, which is much simpler to solve than the smallest one. Using namespace std;char* minstr (char *str,char A1,char a2) {char *p = Str;char *q = Str+strlen ( STR) -1;while (1) {if (*p==a1 | | *p==a2) break;p++;} while (1) {if ((*P==A1) && (*q==a2)) | | ((*P==A2) && (*Q==A1))) break;q--;} *q= ' + '; return p;} int main () {char a[]= "BABBBBBBBCC"; Cout<<minstr (A, ' a ', ' C ') <<endl;return 0;} ----------------------------------------------------------------#include <iostream>using namespace std;// Removes a specific character (string) from the string. Title: Enter two strings and remove all characters from the second string from the first string. For example, enter "they is students." and "Aeiou",//The first string after deletion becomes "Thy R stdnts." Char *grial (char *str,char *s1) {char *p = Str;char *q = S1;while (*q!= '} ') {while (*p!= '} ') {while (*p!=*q && *p!= ' + ') {p++;} Char *m = p;while (*m!= ') {*m=* (m+1); m++;}} q++;p =str;} return str;} int main () {char a[]= "123456 23A AB234"; char b[]= "All"; Cout<<grial (A, b) <<endl;}

C + + Solve string (minimum substring, maximum substring, delete specified string)

Related Article

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.