C ++ calculates the character that appears only once for the first time.

Source: Internet
Author: User

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 <

    

   

  

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.