ACM题目————STL练习之求次数

题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1112

描述

题意很简单,给一个数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

map解法:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Asimple
 
#include <iostream>
#include <map>
#include <cstdio>
 
using namespace std;
int T, n, ans;
string str;
 
int main()
{
    scanf("%d",&T);
//    cin >> T;//TLE了三次,没想到竟然是 cin 的错
    while( T-- )
    {
        cin >> n >> str ;
        ans = 0;
        map<string,int> m;
        int len = str.length();
        for(int i=0; i<=len-n; i++)
        {
            string s = str.substr(i,n);
            if( m[s] == 1 ) ans ++ ;
            else m[s] = 1 ;
        }
        cout << ans << endl ;
    }
 
    return 0;
}

 

set解法:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Asimple
 
#include <iostream>
#include<cstdio>
#include<cstring>
#include <set>
using namespace std;
 
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n,i, ans = 0;
        scanf("%d", &n);
        set<string> S;
        string str, s;
        cin >> str;
        int len = str.length();
        int lens=str.size();
        for(i=0; i<=len-n; i++)
        {
            s=str.substr(i,n);
            S.insert(s);
            if(S.size()==lens) ans++;
            else lens=S.size();
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

最优解,结构字符串排序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Asimple
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
struct str
{
    char s[12];
} r[100001];
 
int n;
char _str[100001];
 
void _strcpy(int x, int y)
{
    int j;
 
    for (j=0; j<n; j++)
        r[x].s[j] = _str[y+j];
    r[x].s[j] = '\0';
    return ;
}
 
bool cmp(str a, str b)
{
    return strcmp(a.s, b.s) < 0;
}
 
int main()
{
    int T, i, count, l;
    scanf("%d", &T);
    while (T--)
    {
        count = 0;
        memset(_str, '\0', sizeof(_str));
        scanf("%d", &n);
        scanf("%s", _str);
        l = strlen(_str);
        for (i=0; i<=l-n; i++)
            _strcpy(i, i);
        sort(r, r+l-n+1, cmp);
 
        for (i=0; i<l-n; i++)
            if (strcmp(r[i].s, r[i+1].s) == 0)
                count++;
        printf("%d\n", count);
    }
    return 0;
}

 

posted @   Asimple  阅读(311)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示