1501110919-Blue Bridge cup-algorithm training string statistics

Source: Internet
Author: User

Algorithm training string Statistics time limit: 1.0s memory limit: 512.0MBThe problem description is given a string of length n, and there is a number L, with a statistic length greater than or equal to the most occurrences of the substring (different occurrences can intersect), if there are multiple, output the longest, if there are still more than one, the output first appears the earliest. Input format first line one number L.
The second line is the string s.
L is greater than 0 and does not exceed the length of S. The output format is a line, and the title requires a string.

Input Sample 1:
4
Bbaabbaaaaa

Output Example 1:
Bbaa

Input Sample 2:
2
Bbaabbaaaaa

Output Example 2:
AA data size and conventions n<=60
All characters in s are lowercase English letters.

Tips
Enumerate all possible substrings, count occurrences, and find the one that meets the criteria. Thinking of solving problemsjust like the topic Tip says, enumerate all the possible substrings, and note that the topic isa substring of length greater than or equal to L is used to control the change of L with a layer of loops. A substring of length n can have len-n in the total string length len. Defines a struct to store various forms of substrings, their lengths, and their occurrences.        Finally, according to the topic requirements: If there are multiple, output the longest, if there are still more than one, the output first appears the earliest sort. Code
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std;struct Chuan{char s[70 ];int Sum;int lens;//This is to record the number of different substring lengths}ss[2000];//n + (n+1) Number of +...+len, up to 1+2+...+60 BOOL CMP (Chuan A,chuan b) {if ( A.sum!=b.sum) return a.sum>b.sum;else return a.lens>b.lens;} Title Requirements: If there are multiple, the output is the longest, if there are still more than one, the output first appears the earliest. Char Sss[70];char now[70];int main () {int n;int len;int ok;int i,j,k,l;int num;scanf ("%d", &n); GetChar (); scanf ("%s", SSS); Len=strlen (SSS); memset (ss,0,sizeof (ss)); num=0;//stores a total of several seed strings while (N<=len)//The most frequently occurring substring {for (i=0;i) with a statistic length greater than or equal to L    <len-n;i++) {//Total string length len, substring length n of a total of len-n for (j=i,k=0;j<i+n;j++,k++) now[k]=sss[j];    Sets a temporary array of pre-stored substring ok=1;        for (l=0;l<num;l++) if (strcmp (NOW,SS[L].S) ==0) {//If the same substring exists, the number of that string is +1 ok=0;        ss[l].sum++;        Break    } if (ok==1)//Otherwise it is added as a new substring {strcpy (ss[num].s,now);    ss[num].sum++;    Ss[num].lens=n;    num++;    } memset (Now,0,sizeof (now)); }   n++; }sort (SS,SS+NUM,CMP), for (i=0;i<n;i++) printf ("%c", Ss[0].s[i]);p rintf ("\ n"); return 0;}


1501110919-Blue Bridge cup-algorithm training string statistics

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.