nyoj 1112-求次数 (string, substr(begin, length), map, pair)

1112-求次数


内存限制:64MB 时间限制:1000ms 特判: No
通过数:3 提交数:8 难度: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

C/C++:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <climits>
#include <bitset>
#define PI 3.1415926

using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    while (t --)
    {
        map <string, int> my_map;
        pair <map <string, int> ::iterator, bool> pr;
        int n, ans = 0, len;
        scanf("%d", &n);
        string str;
        cin >>str;
        len = str.size() - n;

        for (int i = 0; i <= len; ++ i)
        {
            pr = my_map.insert(pair<string, int>(str.substr(i, n), 0));
            if (!pr.second)
                ans ++;
        }
        printf("%d
", ans);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/GetcharZp/p/9364240.html