[51NOD1127]最短的包含字符串(尺取法)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1127
思路:尺取法,一开始我考虑更新右指针,直到遇到一个和l指针指向的字符相同的时候为止,发现这样做ac不了。于是换了一个思路。
一直更新r指针,直到所有字符都出现了一遍后,更新答案和左指针,导致有一个缺口,这时候再更新r指针。
/*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f7f, sizeof(a))
#define lrt rt << 1
#define rrt rt << 1 | 1
#define pi 3.14159265359
#define RT return
#define lowbit(x) x & (-x)
#define onenum(x) __builtin_popcount(x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<LL, LL> pll;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; const int maxn = ;
int ascii[];
char s[maxn];
int n; int main() {
// FRead();
while(~Rs(s)) {
Cls(ascii);
n = strlen(s);
if(n < ) {
printf("No Solution\n");
continue;
}
int ret = 0x7f7f7f;
int cnt = ;
int l = , r = ;
ascii[s[l]]++; cnt = ;
while(r < n) {
if(ascii[s[r]] == ) cnt++;
ascii[s[r]]++;
while(cnt == ) {
ret = min(ret, r-l+);
ascii[s[l]]--;
if(ascii[s[l]] == ) cnt--;
l++;
}
r++;
}
if(ret == 0x7f7f7f) printf("No Solution\n");
else printf("%d\n", ret);
}
RT ;
}
[51NOD1127]最短的包含字符串(尺取法)的更多相关文章
- 51nod1127 最短的包含字符串 尺取法
Bryce1010模板 #include <bits/stdc++.h> using namespace std; typedef long long LL; map<char,LL ...
- 51Nod 1127 最短的包含字符串 (尺取法)
#include <iostream> #include <algorithm> #include <string> #include <cstring> ...
- 51 nod 1127最短的包含字符串(尺取法)
1127 最短的包含字符串 收藏 关注 给出一个字符串,求该字符串的一个子串S,S包含A-Z中的全部字母,并且S是所有符合条件的子串中最短的,输出S的长度.如果给出的字符串中并不包括A-Z中的全 ...
- 字符串----hiho字符串(尺取法)
注意:这道题的解法和最短摘要一样,都是采用尺取法解决问题,注意这儿题目要求恰好包含,也就是说这个hiho字符串必须包含2个'h'.1个'i'和1个'o'.一个不能多,一个也不能少. import ja ...
- 51Nod - 1127 最短的包含字符串
给出一个字符串,求该字符串的一个子串s,s包含A-Z中的全部字母,并且s是所有符合条件的子串中最短的,输出s的长度.如果给出的字符串中并不包括A-Z中的全部字母,则输出No Solution. Inp ...
- 校内模拟赛T5:连续的“包含”子串长度( nekameleoni?) —— 线段树单点修改,区间查询 + 尺取法合并
nekameleoni 区间查询和修改 给定N,K,M(N个整数序列,范围1~K,M次查询或修改) 如果是修改,则输入三个数,第一个数为1代表修改,第二个数为将N个数中第i个数做修改,第三个数为修改成 ...
- 51nod1127(尺取法)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1127 题意:中文题诶- 思路:尺取法 维护一个队列,若当前队 ...
- CF 701C They Are Everywhere(尺取法)
题目链接: 传送门 They Are Everywhere time limit per test:2 second memory limit per test:256 megabytes D ...
- nyoj133_子序列_离散化_尺取法
子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...
随机推荐
- 【BZOJ】【4010】【HNOI2015】菜肴制作
拓扑排序 这题是要求N个点的一个拓扑序,且满足以下条件:编号1的位置尽可能靠前,在此基础上编号2的位置尽可能靠前…… 我看到这题的第一感觉:将拓扑排序用的队列改为优先队列,编号越小越早出来. 但是连样 ...
- Springmvc jar包介绍
spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-m ...
- 驱动笔记 - ioctl
#include <linux/ioctl.h> 定义命令 _IO(type,nr) 没有参数的命令 _IOR(type,nr,datatype) 从驱动中读数据 _IOW(type,nr ...
- mysql去除重复查询的SQL语句基本思路
SELECT R.* FROM trans_flow R, (SELECT order_no, MAX(status_time) AS status_time FROM trans_flow GROU ...
- 服务接口API限流 Rate Limit
一.场景描述 很多做服务接口的人或多或少的遇到这样的场景,由于业务应用系统的负载能力有限,为了防止非预期的请求对系统压力过大而拖垮业务应用系统. 也就是面对大流量时,如何进行流量控制? 服务接口的流量 ...
- HDU 4148 Length of S(n)(字符串)
题目 字符串处理 题意要猜,解析见代码: /* 这题每个S(n)是描述S(n-1)值 例如: S(1)=1; S(2)=11;即描述S(1)有1个1=11 S(3)=21;即描述S(2)有2个1=21 ...
- 【linux】打印字符串中指定行
只打印第10行 关键在于当行数小于10时不输出. 用 head tail的关键问题是当行数小于10的时候用 head -n 10 只会打出前面的行,再用tail就错了. 所以要知道源文件一共有多少行. ...
- CPLD VS FPGA
FPGA(Field-Programmable Gate Array),即现场可编程门阵列,它是在PAL.GAL.CPLD等可编程器件的基础上进一步发展的产物.它是作为专用集成电路(ASIC)领域中的 ...
- 相对布局RelativeLayout
一. public class RelativeLayout extends ViewGroup java.lang.Object ↳ android.view.View ↳ an ...
- 传说中的WCF(3):多个协定
我们知道,WCF服务端是先定义服务协定,其实就是一个接口,然后通过实现接口来定义服务类.那么,有一个问题,如果一个服务类同时实现N个接口(也就是有N个协定)呢?结果会如何? 不必猜,我们还是通过实验来 ...