C + + Programming practice __linux

Source: Internet
Author: User
the left and right movement of characters TopicString is any combination of * and letter, design algorithm, the * are moved to the left, the letters are moved to the rightmost and keep the relative order unchanged. ImplementReverse-process strings. Two pointers, one pointer Alpha initially points to the rightmost character, and the other last pointer initially points to the rightmost *. When Alpha points to a letter, it swaps with the character that was pointed to last, and the two pointers move to the left one at a time. When Alpha points to *, only alpha moves one bit to the left. When Alpha<0, the end. Code
void Change (string& s)
{
    int len = s.length ();
    int alpha = len-1;
    Find the rightmost letter while
    (alpha>=0&&s[alpha] = = ' * ')
        alpha--;
    int last = len-1;
    while (Alpha >= 0)
    {
        if (S[alpha]!= ' * ')
        {
            swap (s[alpha--], s[last--]);
        else
            alpha--;
    }
}
Matching Strings theSupport. and * The regular expression match. Matches any character, * matches 0 or more preceding characters. The string p is a match pattern, and S is the string to match. ImplementRecursion, from the back to the forward comparison. I,j are the positions of the characters currently considered by S and P, and the initial values are slen-1 and plen-1 respectively. According to P[j] is., * or character-splitting considerations:
When p[j]= '. ' The (S,I,P,J) returns True if (S,i-1,p,j-1) matches return true. When p[j]= ' * ', if (s,i-1,p,j-2) match returns True, then (S,I,P,J) returns True, otherwise the remainder of the S character is traversed sequentially, if the character that traverses to s subscript K satisfies the s[k]==p[j-1] or p[j]== '. '. (s,k-1,p,j-2) returns True, (S,I,P,J) also returns True. When P[j] is a different character, (s,i,p,j) returns true only if S[I]==P[J] and (s,i-1,p,j-1) returns True. Code
BOOL IsMatch (String &s,string &p)
{return help
    (S,s.length () -1,p,p.length ()-1);
}

BOOL Help (String &s,int i,string &p,int j)
{
    //special case handling: if P has a length of 0, only the length of S is 0 o'clock to return true
    if (j==-1)
        return i==-1;

    if (p[j]== '. ')
        Return I>=0&&help (s,i-1,p,j-1);
    if (p[j]== ' * ')
    {
        if (s,i-1,p,j-2)) return
            true;
        for (int k=i;k>=0;k--)
        {
            //' * ' in sequence match 1, 2, 3, ... The same first character p[j-1],
            //until S[k] and p[j-1] do not match.
            if (s[k]==p[j-1]| | p[j-1]== '. ')
            {
                if (s,k-1,p,j-2) return
                    true;
            }
            else return
                false;
        }
    else
    {
        if (I>=0&&help (s,i-1,p,j-1)) return
            true;
    }
    return false;
}
string Space compression theCompresses a contiguous space in a string into one and prints the string in reverse order, separated by a space. ImplementIstringstream: Used to work with each word within the line. #
void reverse (string &s)
{
    int len=s.length ();
    if (len<=1) return
        ;
    for (int i=0;i<len/2;i++)
    {
        swap (s[i],s[len-i-1];
    }
}

void Comp (string &s)
{
    Istringstream is (s);
    string Word;
    S.clear ();
    while (Is>>word)
    {
        reverse (word);
        S=s+word+ ';
    }
    S.pop_back ();
}
compression of repeating characters theCompresses the repeated occurrences of letters in a string, outputting the compressed string as required. For example, string xxyyyyyz, compressed for 2x5yz, where Z appears only once so does not compress. ImplementTraversal AH traversal Code
Converts an int to a string
num2str (int n)
{
    string ans;
    if (n==0) return
        "0";
    while (n)
    {
        char tmp= ' 0 ' +n%10;
        n/=10;
        Ans=tmp+ans;
    }
    return ans;
}

String dup (string &s)
{
    int count=1;
    string ans;
    for (int i=1;i<s.length (); i++)
    {
        if (s[i]==s[i-1))
            count++;
        else
        {
            if (count==1)
                ans+=s[i];
            else
                ans=ans+num2str (count) +s[i];
            Count=1
        }
    }
    if (count==1)
        ans+=s[i];
    else
        ans=ans+num2str (count) +s[i];
    return ans;
}
the first character that appears only once theIn a string, find the first character that appears only once. such as input Abaccdeff, output b ImplementStupid way: In the character and the appearance of the subscript set up mapping. There is no such word Fuzega in, no one let this character corresponding subscript set to-1. Code
It is assumed that this only occurrence of characters must exist in
char Onlyfirst (string &s)
{
    if (s.length () ==1) return
        s[0];
    map<char,int>mp;
    for (int i=0;i<s.length (); i++)
    {
        if (Mp.count (s[i))
            mp[s[i]]=-1;
        else
            mp[s[i]]=i;
    }
    int minindex=s.length ();
    for (auto Iter=map.begin (); Ier!=map.end (); iter++)
    {
        if (iter->second!=-1)
        {
            if (iter-> Second<minindex)
                minindex=iter->second;
        }
    }
    return s[minindex];
}

Who has a better way. Delete a specific character Given the original character s and the pattern string p, it is required to delete all characters that appear in the pattern string in the original character. implemented in the original string, the characters appearing in the pattern string are overwritten with the characters that follow. After the match is completed, the last character of the original string is assigned the value '. A string is a sequential storage structure, with each character removed, followed by a move to the front, so that the time complexity is very high. Code

void deletestring (String &s,string &p) {int slen=s.length ();
    int Plen=p.length (); if (slen==0| |
    plen==0) return;
    int hash[256]={0};
    for (int i=0;i<plen;i++) hash[p[i]]=1;
    int slow=0;
    int fast=0;
        while (Fast!=slen) {if (hash[s[fast]]!=1) {s[slow++]=s[fast];
    } fast++;
} s=s.substr (0,slow); }

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.