number of times
Time Limit:
1000
ms | Memory Limit:
65535
KB
Difficulty:
2
- describe
-
The meaning of the question is very simple, given a number n and a string str, the interval [i, i+n-1] is a new string, and 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
ask for ans;
- enter
-
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, and the cut length is less than 10w - output
- ask ans
- sample input
-
2 2 aaaaaaa 3 acmacm
- Sample output
-
5 1
#include<stdio.h> #include<set> #include<cstring> #include<iostream> using namespace std; intmain() { int t,n,years; scanf("%d",&t); while(t--) { years=0; scanf("%d",&n); set<string> s; string s1,s2; cin>>s1; int len=s1.length(); int lens=s.size();//Use the size method to get the size of the S collection object. for(int i=0;i<len-n+1;i++) { s2=s1.substr(i,n);//Return a string of n characters starting from the i-th position s.insert(s2);//Insert s2 into s if(s.size()==lens) //Compare the size of the set set before inserting the substring with the size after that to determine whether it has occurred ans++;//If it appears, the collection size will not increase else lens=s.size(); } printf("%d\n",ans); } return 0; }