题目地址: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解法:

//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解法:

//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;
}

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

//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;
}

ACM题目————STL练习之求次数的更多相关文章

  1. ACM题目————STL练习之众数问题

    描述 所谓众数,就是对于给定的含有N个元素的多重集合,每个元素在S中出现次数最多的成为该元素的重数, 多重集合S重的重数最大的元素成为众数.例如:S={1,2,2,2,3,5},则多重集S的众数是2, ...

  2. ACM题目————STL练习之Ananagrams

    Description Most crossword puzzle fans are used to anagrams--groups of words with the same letters i ...

  3. ACM题目————STL练习之字符串替换

    描述 编写一个程序实现将字符串中的所有"you"替换成"we" 输入 输入包含多行数据 每行数据是一个字符串,长度不超过1000 数据以EOF结束 输出 对于输 ...

  4. ACM题目————STL练习之 懒省事的小明(优先队列)

    描述 小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.小明决定把所有的果子合成一堆. 因为小明比较懒,为了省力气,小明开始想点子了: 每一 ...

  5. ACM题目————STL + 全排列

    今天碰到一个函数,感觉挺好用的,全排列函数 next_permutation! 求全排列的函数,基本上与自己写的DFS时间复杂度差不多,毕竟是标准库.(2018-1-4 添加) 话不多说,直接上题. ...

  6. nyoj 1112 求次数(map, set)

    求次数 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个新的字符串,i 属于[0,strl ...

  7. nyoj 1112 求次数 (map)

    求次数 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个新的字符串,i 属于[0,strl ...

  8. LeetCode通关:求次数有妙招,位运算三连

    分门别类刷算法,坚持,进步! 刷题路线参考: https://github.com/chefyuan/algorithm-base 大家好,我是刷题困难户老三,这一节我们来刷几道很有意思的求次数问题, ...

  9. ACM题目———— 一种排序(STL之set)

    描述 输入 第一行有一个整数 0<n<10000,表示接下来有n组测试数据:每一组第一行有一个整数 0<m<1000,表示有m个长方形:接下来的m行,每一行有三个数 ,第一个数 ...

随机推荐

  1. php下载文件,解压文件,读取并写入新文件

    以下代码都是本人在工作中遇到的问题,并完成的具体代码和注释,不多说,直接上代码: <?php      //组织链接      $dataurl = "http://118.194.2 ...

  2. HDU 5727 - Necklace - [全排列+二分图匹配][Hopcroft-Karp算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5727 Problem DescriptionSJX has 2*N magic gems. ...

  3. POJ 2528 - Mayor's posters - [离散化+区间修改线段树]

    题目链接:http://poj.org/problem?id=2528 Time Limit: 1000MS Memory Limit: 65536K Description The citizens ...

  4. Google词向量word2vec的使用

    """ 1.在自然语言处理中常常使用预训练的word2vec,这个预训练的词向量可以使用google的GoogleNews-vectors-negative300.bin ...

  5. python调用exe程序

    最近在做测试,公司的产品做成了exe,让我去测试,C++写的程序啊,我直接用python调用那个exe,也有个坑,必须要到exe在的那个目录下,然后才能调用: import os def main() ...

  6. Python:fromkeys()方法

    简介 Python 字典(Dictionary) fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值. 语法 fromkeys()方法语法: ...

  7. The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:

    运行环境: Intellij idea 14 在改了项目名称. 运行时候出现了 The APR based Apache Tomcat Native library which allows opti ...

  8. Shell初学(三)传参

    一. 脚本代码:test.sh echo "Shell 传递参数实例!"; echo "执行的文件名:$0"; echo "第一个参数为:$1&quo ...

  9. 非受限联合体 - 现代C++新特性总结

    非受限联合体 非受限联合体:C++98中并不是所有数据类型都能够成为union的数据成员,不允许联合体拥有非POD(Plain Old Data).静态或引用类型的成员. C++11中取消了联合体对于 ...

  10. [原创]nginx添加module之threads

    一.安装nginx yum安装nginx 折叠源码 1 2 3 4 5 6 7 8 9 10 11 12 # 添加nginx源 rpm -ivh http://nginx.org/packages/c ...