Reprint Please specify the Source: http://blog.csdn.net/ns_code/article/details/27110873
The sword refers to a string-related topic on offer.
title: Enter two strings and remove all characters from the second string from the first string. Like what. Enter "They is students." and the "Aeiou" , the first string after the deletion becomes "Thy R stdnts." .
Here are the main analysis of two aspects:
1. How to infer that those characters are characters that need to be deleted.
Same as very many string problems. Be able to open up a hash array. All initialized to False, the corresponding mapping position of the characters in the second string is set to Ture, indicating that the corresponding characters in these positions need to be deleted in the first string.
2, about the deletion of characters, each time delete one. The subsequent elements are then shifted left one bit, because there may be more characters to delete, and the time complexity of such a method is O (n*n). We have a Delete method for O (n) here.we can imagine that when a character needs to be deleted, we fill it with the characters that follow it, and it is equivalent to the character being deleted. In a detailed implementation, we are able to define two pointers(Pfastand thePslow), which initially points to the starting position of the first character. WhenPfastThe character that is pointed to is the character that needs to be deleted, thenPfastSkip directly. Points to the next character. AssumptionsPfastthe character that is pointed to is the character that does not need to be deleted, thenPfastpoint to the word assigned value toPslowpointing to the character, andPfastand thePstartmove backwards at the same time to point to the next character. Such Front wasPfastthe skipped characters are equivalent to being deleted. In such a way, the whole deletion inO (n)can be completed within the time.
The deletion method is also used in the previous post that removes the repeated characters. See here: http://blog.csdn.net/ns_code/article/details/21328151.
In addition, there is a point to note. The range of char in the range of -128-127,unsigned char is at 0-255. Therefore the ASCII value between 128-255 characters is assumed to be saved for char type. The range of conversions to int values is between -128--1, which is reflected in the following code.
The code written according to the above ideas is as follows:
#include <stdio.h> #include <string.h> #define MAX 256void deletechars (char *str1,char *str2) {if (str1==null || Str2==null) Return;bool Hashtable[max];memset (Hashtable,0,sizeof (Hashtable));// Set the value of the position in the corresponding Hashtable array of the character in str2 to Turewhile (*str2! = ') {//ascii value is between 128-255 characters. Saved with Char, converted to int, between -128--1 int index;if (*str2 >= 0) index = *str2;elseindex = *str2 + 256;hashtable[index] = true;++st R2;} Char *pfast = Str1;char *pslow = Str1;while (*pfast! = ') ') {int index;if (*pfast >= 0) index = *pfast;elseindex = *pfast + 256;//Whether or not you encounter the character to be deleted, the pfast moves back,//only if there is a character to be deleted, Pslow moves back if (!hashtable[index]) *pslow++ = *pfast;++pfast;} *pslow = ' + ';} int main () {char str1[] = "They is students", char str2[] = "Tt";D eletechars (STR1,STR2);p UTS (STR1); return 0;}
Test results:
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
"Sword means offer" the delete character also appears in a string