nyoj1112 number of times (sort struct string)

Source: Internet
Author: User
Tags strcmp

    • topic 1112
    • topic information
    • run result
    • line
    • discussion area
Time limit for times of evaluation: +Ms | Memory Limit:65535KB Difficulty:2
Describe

Test instructions is simple, give a number n and a string str, the interval "i,i+n-1" for a new string, I belongs to "0,strlen (str)" If the new string appears ans++, for example: ACMACM n=3, then the substring is ACM CMA mac ACM, Only ACM has appeared.

Seeking ans;

Input
Line 1:t Group data (T<10)
Line 2:n, n <= 10, and less than strlen (str);
Line 3:str
STR contains only English lowercase letters, cut lengths less than 10w
Output
Ask for ans
Sample input
22aaaaaaa3acmacm
Sample output
51
Uploaded by

Acm_ Wang Yarong

The first time I saw 10W I thought it would time out, but it didn't.

is to split the string and then store the segmented string in the struct (a bit embarrassing.) will only use the structure of the string sort, qsort forget ... )

The structure of the string is then sorted, if there are two strings of the same ans++;

Code:

#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define NUM 100005char Str[num],temp[11];struct Node {char dir[11];} C[num];bool CMP (node X,node y)//String ordering of structs {if (strcmp (x.dir,y.dir) <0) return True;return false;} int main () {int test,n,i,t,len;scanf ("%d", &test), while (test--) {scanf ("%d%s", &n,str); Len=strlen (str); i=0; while (I+n<=len)//split string and store string {t=0;for (int q=i;q<i+n;q++) temp[t++]=str[q];temp[t]= ' + '; strcpy (c[i].dir,temp ); memset (temp,0,sizeof (temp)); i++;} Sort (c,c+i,cmp), int ans=0;for (int j=1;j<i;j++) if (strcmp (c[j].dir,c[j-1].dir) ==0) ans++;p rintf ("%d\n", ans);} return 0;}


nyoj1112 number of times (sort struct string)

Related Article

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.