String problem (i)

Source: Internet
Author: User
string Problems

1. Left spin problem

2. Character Inclusion issues

3. Character Matching KMP

4. Editing distance

5. Maximum palindrome string, common substring

6. Maximum common sub-sequence, Palindrome subsequence, ascending sub-sequence

7. Basic String Function implementation

8. addition, subtraction, multiplication, addition, modulus of large integers

9. Legal Palindrome, digital string

10. Regular match, longest common prefix, simplified routing


1) left-handed string

Defines the left rotation of a string: moves several characters in front of the string to the end of the string, such as ABCdef the string to the left 2 bits to get the string cdefab. A function that implements the left rotation of the string requires that the time complexity of the string operation of length n is O (n) and that the spatial complexity is O (1).


idea One, violent displacement law

Voidleftshiftone (char *s,int N) {

Char t = s[0];

Save first character

for (int i = 1; I <n; ++i) {

S[I-1] = s[i];

}

S[n-1] = t;

}

So, if you move the M-bit left, you can do the following:

void Leftshift (Char*s,int n,int m) {

while (m--) {

Leftshiftone (S,n);

}

}


thinking Second, the hand-flipping method

#include<iostream>

#include<string>

using namespacestd;

void Rotate (STRING&STR,intm) {

if (str.length () = = 0 | | m <= 0)

return;

intn = str.length ();

if (m% n <= 0)

return;

intp1 = 0, p2 = m;

intk = (n-m)-n% m;

Swap the element that p1,p2 points to, and then move the P1,P2

while (k--) {

Swap (STR[P1],STR[P2]);

p1++;

p2++;

}

Focus, all in the following lines.

Handle tail, R is the number of trailing left shift

intr = n-p2;

while (r--) {

inti = p2;

while (i> p1)

{

Swap (str[i],str[i-1]);

i--;

}

p2++;

p1++;

}

Like an example, Abcdefghijk

P1p2

When executed here, Defghia b c J k

P2+m out of Bounds,

r=n-p2=2, so the following procedure, to execute the loop two times.

First time: J step forward, Abcjk->abjck->ajbck->jabck

Then, p1++,p2++,p1 refers to A,p2 K.

P1 P2

Second time: Defghij a b C K

Likewise, after that, K moves forward, abck->abkc->akbc->kabc.

}

A few different processing optimizations were made in the tail processing

void Rotate (STRING&STR,intm) {

if (str.length () = = 0 | | m < 0)

return;

Initialize P1,P2

intp1 = 0, p2 = m;

<

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.