南阳oj 求次数(题目1112)

1、题目信息

求次数

时间限制: 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

样例输入

2
2
aaaaaaa
3
acmacm

样例输出

5
1

2、思路

使用c++的STL中的set类,set集合会自动去除重复元素,所以比较前后集合的长度,如果不变就是出现了相同

3、代码

#include <iostream>
#include <set>
#include <string>
using namespace std;
int main(){
    int n,m,len,ans;
    string str;
    cin>>n;
    while(n--){
        set <string> s;
        cin>>m>>str;
        ans=0;
        for(int i=0;i+m-1<str.length();i++){
            len=s.size();
            s.insert(str.substr(i,m));
            if(len==s.size())
                ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/sicauliuy/article/details/80146460
今日推荐