Codeforces 92C【二分】
意:
求最少需要几个s1串拼接存在子串s2 (1≤|s1|≤1e4,1≤|s2|≤1e6).
思路(感谢ZQC):
每个字母的出现位置存个vector.
假设你当前已经用了A串的前x个字符,现在想要匹配一个'x',那你就在'x'的vector那里二分出第一个大于x的位置,
如果匹配不到的话,就相当于需要一个新的串。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; vector<int>xs[30];
char s1[10010],s2[1000010];
map<int,int>mp; int main()
{
int x,n,m,pos,ans,k;
vector<int>::iterator y;
scanf("%s%s",s1,s2);
n=strlen(s1);
for(int i=0;i<n;i++)
{
x=s1[i]-'a';
xs[x].push_back(i);
mp[x]=1;
}
m=strlen(s2);
pos=-1;
ans=1;
for(int i=0;i<m;i++)
{
x=s2[i]-'a';
if(!mp[x])
{
puts("-1");
return 0;
}
y=upper_bound(xs[x].begin(),xs[x].end(),pos);
if(y==xs[x].end())
{
ans++;
y=xs[x].begin();
}
k=y-xs[x].begin();
pos=xs[x][k];
}
printf("%d\n",ans);
return 0;
}
Codeforces 92C【二分】的更多相关文章
- CodeForces - 363D --二分和贪心
题目:CodeForces - 363D 题意:给定n个学生,其中每个学生都有各自的私己钱,并且自己的私己钱只能用在自己买自行车,不能给别人. 给定m个自行车,每个自行车都有一个价格. 给定公有财产a ...
- Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论
n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...
- Codeforces 732D [二分 ][贪心]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...
- codeforces 359D 二分答案+RMQ
上学期刷过裸的RMQ模板题,不过那时候一直不理解>_< 其实RMQ很简单: 设f[i][j]表示从i开始的,长度为2^j的一段元素中的最小值or最大值 那么f[i][j]=min/max{ ...
- CodeForces - 551C 二分+贪心
题意:有n个箱子形成的堆,现在有m个学生,每个学生每一秒可以有两种操作: 1: 向右移动一格 2: 移除当前位置的一个箱子 求移除所有箱子需要的最短时间.注意:所有学生可以同时行动. 思路:二分时间, ...
- Robin Hood CodeForces - 672D (二分)
大意: 给定数组$a$, 每次操作使最大元素减小1最小元素增大1, 求k次操作后最大值与最小值的差. 二分出k次操作后最大值的最小值以及最小值的最大值, 若和能平分答案即为$max(0,R-L)$, ...
- Queries about less or equal elements CodeForces - 600B(二分)
You are given two arrays of integers a and b. For each element of the second arraybj you should find ...
- Enduring Exodus CodeForces - 655C (二分)
链接 大意: n个房间, 1为占用, 0为未占用, John要将k头奶牛和自己分进k+1个空房间, 求John距最远的奶牛距离的最小值 这种简单题卡了20min.... 显然对于固定的k+1个房间, ...
- CodeForces 483B 二分答案
题目: B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- 在解析XML时要注意解析元素和解析标签属性的区别
解析元素时: Sting str = ele.elementText("name"); 而解析标签属性时: String key = ele.attributeValue(&quo ...
- java:Maven构建项目速度太慢的解决办法,以及报错Retrieving archetypes:' has encountered a problem
如果报错信息如下: Retrieving archetypes:' has encountered a problemAn internal error occurred during:"R ...
- hdu 2955 Robberies(01背包)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- django admin扩展user表password验证及set_password
一般如果扩展了django user内置表,在admin后台创建新用户的时候密码将会变成明文,故而导致登录不成功.所以我们在admin.py可以通过form自定义进行对password进行操作,可以双 ...
- 一个用 vue 写的树层级组件 vue-ztree
最近看了大神的关于vue-ztree的博客,感觉很赞,于是摘抄下来,方便自己学习,机智girl,哈哈哈O(∩_∩)O 最近由于后台管理项目的需要,页面需要制作一个无限树的需求,我第一感就想到了插件 z ...
- Opencv - Android 配置安装
1.道具们: windows 7 64位 OpenCV-2.4.6-android-sdk-r2 ( http://sourceforge.net/projects/opencvlibrary/fil ...
- OpenCv-Python 图像滤波
均值滤波 均值滤波函数cv2.blur() import cv2 img = cv2.imread('01.jpg') blur = cv2.blur(img,(5,5)) cv2.imshow( ...
- Mybatis学习--Java API
学习笔记,选自Mybatis官方中文文档:http://www.mybatis.org/mybatis-3/zh/java-api.html#directoryStructure 既然你已经知道如何配 ...
- linux vsftpd 服务配置
vsftpd.conf配置如下: # Example config file /etc/vsftpd/vsftpd.conf # # The default compiled in settings ...
- IronPython for ASP.NET 部署注意事项
用 IronPython for ASP.NET 开发的网站,在部署时,除了发布 bin 目录下的 IronPython.dll, IronMath.dll, Microsoft.Web.IronPy ...