[OJ] removes duplicates from strings and extracts duplicate characters.

Source: Internet
Author: User

[OJ] removes duplicates from strings and extracts duplicate characters.

A simple string question on ACM, I found similar code on the Internet for reference and personal thoughts.

 

Theme
You can enter a string to search for repeated characters. Delete the repeated characters in the original string and output them in the original order.
Input
Output
Empty line between two outputs
Sample Input
 ads_fagaerididfhdus_afiew
Sample output
 ads_fgerihuw    ads_fei
To see this question, the first idea is to mark it with a flag [100000] array whose initial values are all 0. The first is to traverse the entire string, starting from the I character and all the characters in the entire string (do not separate the I character separately, it may be a bit difficult to calculate later, when determining, the flag [I] = 1) is used for one-to-one comparison. If there are duplicates, the flag [I] ++ is used. Finally, the flag [I] value is used for determination. If falg [I] = 1, it is printed to the screen, but it only prints out non-repeated characters, duplicate characters are not deleted and are not printed on the screen. This obviously does not meet the requirements of the question.

What should we do? Is it a comparison between the I character and the character before or after the character when traversing the string? If so, it can be determined based on the flag [I] of each repeated character, you only need to print the flags [I] = 0 and flags [I] = 2 to the screen. In this case, the first requirement in the question is to delete repeated characters and print them to the screen in order. But how can we do the second one to extract repeated characters in order? Because repeated characters may not appear twice, the flag [I] may be 1, 2, 3, 4, and so on.

By referring to the Code on the Internet, this idea will come out. As we all know, the characters input on the keyboard have corresponding ASCII codes in the ASCII character set. The ASCII codes of the characters are one to one, which can be considered as an array with a capacity of 128, the ASCII character encoding is between 0 and 127. This solution also solves the problem that the character at each position in the previous one requires a flag space, and only 128 of the space is enough.

Under this idea, we will Initialize all the flag [128] ={}. When traversing the string, we first determine whether the flag [str [I] is 0. Because the initial value is 0, the flag corresponding to each character is 0, the value of each flag [str [I] that has been determined and meets the condition is changed to 1 (not 0 ), in this way, when the next identical character is used for judgment, the condition will not be met, and all the conditions that meet the condition will be printed on the screen. In this way, the first requirement of the question is met,

What about the second requirement? Still, it seems like recursion. Proceed with the previous judgment. What if str [I] is rejected? We need to print it once, and there are duplicates in it. OK. In the old routine, the flag [str [I] = 2 that does not meet the preceding conditions. (if it is not set to 1, the value is 3 for differentiation ).

Okay. In this case, the value of the flag [str [I] corresponding to all repeated character ASCII codes is 2, as in the first requirement, all the characters are 0 for the flag [str [I] corresponding to ASCII. You can also apply the Code model in the first requirement. Another for loop uses flag [str [I] = 2 as the judgment condition. If the condition is met, it is changed to 3 (not 2 ). In this case, it doesn't matter if the conditions are not met, because it is useless if the conditions have been met. We can print the matching conditions to the screen.

The Code is as follows:

#include <stdio.h>
#include <string.h>
int main (void)
{
     int i;
     char a [100000];
     int flag [128] = {};
     gets (a);
     for (i = 0; i <strlen (a); i ++)

         if (flag [(int) a [i]] == 0)

         {
             flag [(int) a [i]] = 1;
             printf ("% c", a [i]);
         } // Print after deleting duplicate characters in the original order
         Else
             flag [(int) a [i]] = 2;

         printf ("\ n \ n"); // empty line
     for (i = 0; i <strlen (a); i ++)
         if (flag [(int) a [i]] == 2)
         {
             flag [(int) a [i]] = 3;
             printf ("% c", a [i]);
         } // Print duplicate characters in the original order
}


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.