NYOJ 1112 求次数(map)

来源:互联网 发布:windows c语言编译器 编辑:程序博客网 时间:2024/05/07 13:50

求次数

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述

题意很简单,给一个数n 以及一个字符串str,区间【i,i+n-1】 为一个新的字符串,i 属于【0,strlen(str)】如果新的字符串出现过ans++,例如:acmacm n=3,那么 子串为acm cma mac acm ,只有acm出现过

求ans;

输入
LINE 1: T组数据(T<10)
LINE 2: n ,n <= 10,且小于strlen(str);
LINE 3:str
str 仅包含英文小写字母 ,切长度小于10w
输出
求 ans
样例输入
22aaaaaaa3acmacm
样例输出
51

 


 

题意是在一个长串中找长度为n的字串再次出现的次数(任意长度为n的字串再次出现的总次数)。


#include<cstdio>#include<cstring>#include<string>#include<map>using namespace std;char str[100010];int main(){int t,n,len,i,j;scanf("%d",&t);while(t--){scanf("%d%s",&n,str);map<string,int>m;len=strlen(str);//m.clear();for(i=0;i<=len-n;i++){string s;for(j=i;j<i+n;j++)    s+=str[j];m[s]++;}map<string,int>::iterator it;//定义一个迭代器 int ans=0;for(it=m.begin();it!=m.end();it++)//m.begin():返回指向map头部的迭代器,m.end():返回指向map尾部的迭代器 {int count=it->second;//返回当前元素的值 if(count>=2)   ans=ans+count-1;}printf("%d\n",ans);}return 0;}


 

0 0