//The character that appears only once in the string for the first time. #include <iostream>#include <string.h>#define _SIZE_ 255Using namespace Std;struct node{int Index;//Store subscript. intNum//Storage count. Node ():Index(-1), Num (0){}};CharGrial (Char*Str) {Node node[_size_];//If only 26 characters are considered, there is no need for this amount of space. intSlen = strlen (Str);inti =0;intMix =0x7fffffff; for(; i < slen; i++) {node[Str[i]].Index= i;//Subscript save. node[Str[i]]. num++;//Save number. } for(i =0; i < _size_; i++) {if(Node[i].num = =1) {mix = Mix>node[i].Index? Node[i].Index: Mix;//Find the subscript that appears only once for the first time. } }return Str[Mix];The total time complexity is n (traversed once) +m (total number of occurrences). //Spatial complexity, opening up a set of additional node space to store data. }intMain () {Char*p ="1222345678234543"; Cout<<grial (P) <<endl;return 0;}
C + + to find characters that appear only once for the first time in a string