Asks for the first occurrence of a character in a string.
#include <iostream>
#include <string.h>
#define _SIZE_ 255
using namespace std;
struct Node
{
int index;//store subscript.
int num;//storage number.
Node (): Index ( -1), num (0) {}
};
Char grial (char *str)
{
Node node[_size_];//does not require so much space 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;//subscript save.
number of node[str[i]].num++;//saved.
}
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 (iterate through) +m (total number of occurrences).
//Space complexity, which opens up a set of additional node space to store the data.
}
int main ()
{
char *p = ' 1222345678234543 ';
Cout<<grial (p) <<endl;
return 0;
}