Delete the same element in a string: one procedure

Source: Internet
Author: User
One problem is how to delete two characters with the same string, such

Str1 = "abcdeafg" str2 = "blimklaaaaa"

To get:

Str1 = "cdefg" str2 = "limkl"

Write the program directly below. I wrote this program, but the idea is others '.

For the convenience of discussion, assume that both str1 and str2 are ASCII codes.

  1. Void delsamechs (char * str1, char * str2)
  2. {
  3. // The ASCII code is 0-255 (accurate to 0-127). Therefore, a temporary array is defined and its subscript is ASCII code.
  4. // Its element is the number of times that subscript I appears in str1 and str2
  5. Int temp [256];
  6. Char * pstr1 = str1;
  7. Char * pstr2 = str2;
  8. Memset (& temp, 0, sizeof (temp); // clear 0
  9. While ('/0 '! = * Pstr1) // traverse str1 and set it to 1 in temp
  10. {
  11. Temp [* pstr1] = 1;
  12. Pstr1 ++;
  13. }
  14. While ('/0 '! = * Pstr2) // traverse str2. If it is 1, set it to 2 in temp. If it is 2, it is the public element of the two strings.
  15. {
  16. If (1 = temp [* pstr2])
  17. Temp [* pstr2] = 2;
  18. Pstr2 ++;
  19. }
  20. For (INT I = 0; I <256; I ++)
  21. {
  22. If (2 = temp [I])
  23. {
  24. Delch (str1, I); // delete all character I in str1 (if any)
  25. Delch (str2, I );
  26. }
  27. }
  28. }

The principle of doing so is to change the space for time and sacrifice the space of 256 integers (which can be replaced by char temp [256 ), A three-time Parallel loop can be used to find the same elements of the two arrays. If the conventional method is used, it is estimated that the nested loop should be 3 times ~

If it is Unicode, you can set int temp [65536];

If it is multi-byte encoding, I have never thought about it ~

The following describes the implementation of void delchar (char * STR, char ch.

Because it is necessary to delete all the I, if it is a general practice, each time you delete an element, the following will move to the front, it will take a lot of time.

Next, let's look at this Code:

  1. Void delchar (char * STR, char ch)
  2. {
  3. Char * pcurr = STR; // used to process the deleted result
  4. Char * ptemp = STR; // used to scan the source string Str
  5. While ('/0 '! = * Ptemp)
  6. {
  7. If (Ch! = * Ptemp)
  8. {
  9. * Pcurr = * ptemp;
  10. Pcurr ++;
  11. }
  12. Ptemp ++;
  13. }
  14. * Pcurr = '/0 ';
  15. }

The idea of this Code is different from that of the conventional code. If you move the code in the past, you don't want to delete it. Instead, you don't want to move it. In this way, you only need to scan once.

In this way, the entire program can complete the task by scanning up to five strings.

It can also be extended. If you delete multiple strings with the same characters, all of them are processed in this way, and the complexity is O (n)

Can it be simplified ??? I cannot think of it now. Please give me more advice. Thank you very much.

Note: The idea of this delchar program is obtained by referring to the unique source code in STL algorithm.

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.