C ++ calculates the character that appears only once for the first time.
// Evaluate the character that appears only once for the first time in the string. # Include
# Include
# Define _ SIZE _ 255 using namespace std; struct Node {int index; // store the subscript. Int num; // Number of buckets. Node (): index (-1), num (0) {}}; char Grial (char * str) {Node [_ SIZE _]; // This space is not required if only 26 characters are considered. Int slen = strlen (str); int I = 0; int mix = 0x7fffffff; for (; I <slen; I ++) {node [str [I]. index = I; // Save the subscript. Node [str [I]. num ++; // number of records saved. } For (I = 0; I <_ SIZE _; I ++) {if (node [I]. num = 1) {mix = mix> node [I]. index? Node [I]. index: mix; // obtain the subscript that appears only once for the first time. } Return str [mix]; // The total time complexity is n (traversal) + m (total number of occurrences ). // Space complexity, opening up a set of additional Node space to store data .} Int main () {char * p = "1222345678234543"; cout <