Chapter 4 exercise questions-7-Minimum prefix-4th

Source: Internet
Author: User
# Include <iostream>
 
# Include <stdio. h> # include <string. h> # include <vector> # include <list> # include <math. h ># include <algorithm> using namespace STD; int main () {char DIC [1001] [21], substr [21], TMP [21]; int I, j, p, K; memset (DIC, 0, sizeof (char) * 1001*21); memset (substr, 0, sizeof (char) * 21); memset (TMP, 0, sizeof (char) * 21); k = 0; while (EOF! = Scanf ("% s", DIC [k]) K ++; for (I = 0; I <K; I ++) {for (j = 1; j <= strlen (DIC [I]); j ++) {strncpy (substr, DIC [I], J); For (P = 0; P <K; P ++) {If (P! = I) {strncpy (TMP, DIC [p], J); If (strcmp (TMP, substr) = 0) Break ;}} if (P <k) continue; if (P = k) break;} printf ("% S % s \ n", DIC [I], substr); memset (substr, 0, sizeof (char) * 21); memset (TMP, 0, sizeof (char) * 21);} return 1 ;}

The time complexity is 1000*20*1000. You can use the Prefix Tree (Dictionary tree)

Brute force thinking:

For each string, the prefix with the length of J (1 <= j <= strlen) is used to determine whether the prefix exists in other strings. If not, the minimum prefix of the string;

When J is equal to the length of the string, it can be output directly without being determined by other strings.

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.