Crazy Search
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1216 Accepted Submission(s): 473
Your task is to write a program that given the size, N, of the substring, the number of different characters that may occur in the text, NC, and the text itself, determines the number of different substrings of size N that appear in the text.
As an example, consider N=3, NC=4 and the text "daababac". The different substrings of size 3 that can be found in this text are: "daa", "aab", "aba", "bab", "bac". Therefore, the answer should be 5.
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
1 3 4 daababac
住借用关联容器,熟悉各种容器的用法!
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
string str;
int t,n,m,i;
int tag=0;
cin>>t;
while(t--)
{
if(tag)
cout<<endl;
tag++;
cin>>n>>m;
cin>>str;
map<string,int>ans;
for(i=0;i<=str.size()-n;i++)
{
string temp(str,i,n);
if(ans.count(temp)==0)
ans[temp]++;
}
cout<<ans.size()<<endl;
}
return 0;
}