C + + to ask for the first occurrence of a string character __c++

Source: Internet
Author: User
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.