BestCoder Round #81 (div.2) 1004 String(动态规划)
题目链接:BestCoder Round #81 (div.2) 1003 String
题意
中文题,上有链接。就不贴了。
思路
枚举起点i,计算能够达到k个不同字母的最小下标j,则此时有子串len-j个。
将全部起点的值加起来即是结果。
代码
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define LL long long
const int MOD = 1000000007;
char str[1000009];
int num[27];
int get_cnt()
{
int cnt = 0;
for(int i=0; i<26; i++)
cnt += num[i]==0?0:1;
return cnt;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int k;
memset(num, 0, sizeof(num));
scanf("%s%d", str, &k);
int len = strlen(str);
LL ans = 0;
int j = -1;
bool flag = false;
for(int i=0; i<len; i++)
{
while(get_cnt() < k)
{
j++;
if(j == len)
{
flag = true;
break;
}
num[str[j]-'a']++;
}
if(flag)
break;
ans += len-j;
num[str[i]-'a']--;
}
printf("%lld\n", ans);
}
return 0;
}
BestCoder Round #81 (div.2) 1004 String(动态规划)的更多相关文章
- BestCoder Round #81 (div.2) 1003 String
题目地址:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=691&pid=1003题意:找出一个字符串满足至少 ...
- BestCoder Round #81 (div.2)C String
总体思路好想,就是在找K个不同字母的时候,卡时间. 看了大神代码,发现goto的!!!!998ms #include<cstdio> #include<cstring> #in ...
- BestCoder Round #81 (div.2) B Matrix
B题...水题,记录当前行是由原矩阵哪行变来的. #include<cstdio> #include<cstring> #include<cstdlib> #inc ...
- BestCoder Round #81 (div.1)A
水题...就是n的三进制后m位 #include<cstdio> #include<cstring> #include<cstdlib> #include<i ...
- BestCoder Round #81 (div.2)1001
Machine Accepts: 580 Submissions: 1890 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...
- BestCoder Round #81 (div.2)
HDU:5670~5764 A题: 是一个3进制计数: #include <bits/stdc++.h> using namespace std; ]; int calc(long lon ...
- hdoj5671 BestCoder Round #81 (div.2)
对于交换行.交换列的操作,分别记录当前状态下每一行.每一列是原始数组的哪一行.哪一列即可. 对每一行.每一列加一个数的操作,也可以两个数组分别记录.注意当交换行.列的同时,也要交换增量数组. 输出时通 ...
- HDU5526/BestCoder Round #61 (div.1)1004 Lie 背包DP
Lie 问题描述 一个年级总共有N个学生,每个人属于唯一一个班级.现在他们站在一排,同班同学并不一定会站在一起,但每个人都会说一句话:“站在我左边的有Ai个同班同学,右边有Bi个同班同学”.然而并 ...
- BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)
Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
随机推荐
- 安卓Eclipse开发人员的福音
我们知道.谷歌已经放弃对Eclipse(ADT)的维护更新了.如今官网上也找不到ADT的下载链接了,我们大多数同学仍在使用的ADT版本号可能已经非常老了,预计大多数的SDK版本号仅仅到4.4,而,在尝 ...
- 阻塞与非阻塞、同步与异步、I/O模型
1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式: 同步/异步主要针对C端: 同步: 所谓同步,就是在c端 ...
- 浅谈Volatile与多线程
标题:浅谈Volatile与多线程 2011-04-19 22:49:17 最近看的比较杂,摘了一些人的笔记!随着多核的日益普及,越来越多的程序将通过多线程并行化的方式来提升性能.然而,编写正 ...
- 为什么用strlcpy取代strncpy
为什么用strlcpy取代strncpy 标签: C, C语言, strlcpy, strncpy 标题: 为什么用strlcpy取代strncpy作者: Demon链接: http://demon. ...
- Nginx视频流模块nginx-rtmp-module
文章参考地址:https://www.cnblogs.com/tinywan/p/6202345.html nginx-rtmp-module:https://github.com/arut/ngin ...
- 基于AndroidPn二次开发的可行性
一.背景 如果要自己搭建,从零开始做或基于开源进行修改扩充,开源的push引擎,90%的博文首推AndroidPN,结合公司现状,最优解决方案就是进行AndroidPN的二次开发了.先看一下这个项目: ...
- linuxubuntu升级.net core版本到2.0
直接看这里 https://www.microsoft.com/net/core#linuxubuntu
- ubuntu下运行第一个.net core web程序
前置条件 ubuntu系统 且已经安装dotnetcore运行环境 mkdir testMVC 创建一个文件夹 cd testMVC 进入文件夹 dotnet new -t web 创建程序( ...
- ASP.NET MVC使用IoC
也许你会问ASP.NET MVC为什么会爱上IoC? 相爱的理由常常很简单,就像一首歌中所唱——“只为相遇那一个眼神”. 而ASP.NET MVC爱上IoC只为IoC能实现MVC控制器的依赖注入. 下 ...
- Python读取键盘输入
Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘.例如以下: raw_input input raw_input函数 raw_input() 函数从标准输入读取一个行.并返回 ...