1-1
To solve the problem of repeated or frequent occurrence in strings, the most common one is the hash table. The character in the string is used as the key, and the number of occurrences of the character is used as the value. It is assumed that there are only characters within the ASCII code range, you can open up a 256-size int array, map each character (key) to the corresponding position of the array, calculate the number of occurrences, and traverse the string once, calculates the number of occurrences of each character, stores it in the corresponding position of the int array, and traverses the string for the second time, if the first occurrence of a character pair corresponds to the corresponding position of the hash table where the element is 1, then this character is the first character that appears only once, if we traverse the hash table (int array), the character corresponding to the position where the first element in the hash table is 1 is the first smallest character in the string that appears only once. The time complexity is O (n), which requires an additional 256 int space. It can be seen that the space complexity is O (1 ).
In addition, to save space, we can use the bitmap Algorithm to represent the number of occurrences of the corresponding character in two bits. If the occurrence is 0, it is 00, and if the occurrence is 01, if there are two or more times, you can maintain them at 10.
In addition, note that the char range is-128-127, and the unsigned char range is 0-255. Therefore, the ASCII value is between 128-255. If it is saved as a char type, the conversion range to int value is between-128--1, which is embodied in the following code.
The following code uses a simple hash table AC (two functions are written according to the question requirements and test requirements ):
# Include
# Include
/* Returns the first occurrence character */char FirstOnceChar (char * str) {if (str = NULL) return '\ 0'; int hashtable [256]; memset (hashtable, 0, sizeof (hashtable); char * pCur = str; while (* pCur! = '\ 0') {if (* pCur> = 0) hashtable [* (pCur ++)] ++; elsehashtable [* (pCur ++) + 256] ++;} while (* str! = '\ 0') {int index; if (* str> = 0) index = * str; elseindex = * str + 256; if (hashtable [index] = 1) return * str; elsestr ++;} return '\ 0';}/* returns the subscript of the first character that appears */int IndexOfFirstOnceChar (char * str) {if (str = NULL) return-1; int len = strlen (str); int hashtable [256]; memset (hashtable, 0, sizeof (hashtable )); int I; for (I = 0; I
= 0) hashtable [str [I] ++; elsehashtable [str [I] + 256] ++ ;}for (I = 0; I
= 0) index = str [I]; elseindex = str [I] + 256; if (hashtable [index] = 1) return I;} return-1 ;} int main () {char str [10010]; while (gets (str )! = NULL) printf ("% d \ n", IndexOfFirstOnceChar (str); return 0 ;}
/*************************************** ***********************
Problem: 1283
User: mmc_maodun
Language: C
Result: Accepted
Time:10 ms
Memory:912 kb
****************************************************************/