当前位置:网站首页>* 3-3 cattle from rearranged
* 3-3 cattle from rearranged
2022-08-09 14:50:00 【Ye Xiaobai】
题目描述

样例

源代码
#include<iostream>
using namespace std;
string s = "puleyaknoi";
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
int a[maxn], b[maxn];
char c[maxn];
int solve() //判断函数,判断元素的个数是否满足题目要求
{
for (int i = 0; i < 26; ++i)
if (a[i] < b[i])
return false;
return true;
}
int main()
{
int t;
scanf("%d", &t);
for (int i = 0; i < s.size(); ++i)
b[s[i] - 'a']++; //记录目标子串每个元素的个数
getchar(); //获取回车,防止干扰后序字符串的输入
for (int i = 1; i <= t; ++i)
{
scanf("%s", c);
int lc = strlen(c);
int r = 0, ans = inf;
for (int l = 0; l < lc; ++l)
{
while (!solve() && r < lc) //寻找目标子串,r一直右移直到当前区间[l,r]内出现目标子串
a[c[r++] - 'a']++;
if (solve()) //一旦出现目标子串就记录一下此时的最小区间
ans = min(ans, r - l);
a[c[l] - 'a']--; //l不断的右移,每次都除去一个最左边的元素,目标就是找到最小区间
}
if (ans == inf) //没有找到
puts("-1");
else
printf("%d\n", ans);
}
return 0;
}
关于这题
刚刚开始看到这个题目的时候,感觉很不好做,唯一想到的方法就是暴力,但是很显然暴力一定会超时,一看题解发现是个尺取法的经典例题,无奈这种题目做的比较少,看懂代码也花了一定的时间,也参考了博客理解尺取法的概念与用法.具体题目分析见代码
The key to solving the problem is solve 这个判断函数
边栏推荐
- C语言 求一个整数存储在内存中的二进制中1的个数(多种方法详解)
- RHCE Course Summary
- Es7.x使用RestHighLevelClient进行查询操作
- *1-5 OJ 642 Russian Multiplication
- 【ClickHouse】 日志清理方法(query_log、query_thread_log)
- 实现H5网页授权
- 汇编语言学习(十)常用指令总结
- Dry+Bean+Dataset R language data analysis, report in English
- *1-2 OJ 190 run-length code
- 阿里巴巴开源大规模稀疏模型训练/预测引擎DeepRec
猜你喜欢
随机推荐
idea安装
Es7.x使用RestHighLevelClient进行增删改和批量操作
Analysis of SEATA Distributed Transaction Framework
Shell course summary
How to adjust the spacing between numbers and text in Word?
Column of openharmony container component
微信小程序getPhoneNumber接口code=40013
*3-2 CCF 2014-09-2 画图
apt-cache command
C语言中的 递归问题 以及将递归改写成非递归。(解析常见的几个递归题目及代码) 求阶乘、求斐波那契、汉诺塔、
元气森林“0糖”背后的百亿推手
LNMP架构搭建之论坛
Jetpack Compose - the use of Image (picture)
*3-3牛客网 重新排列
Minesweeper game
*4-1 CCF 2014-12-1门禁系统
使用Connection对象连接管理事务
Operating system migration practice deploying MySQL database on openEuler
*1-3 OJ 291 老鼠与猫的交易
除了开心麻花,中国喜剧还有什么?









