Educational Codeforces Round 81 (Rated for Div. 2) C. Obtain The String
题目链接:http://codeforces.com/contest/1295/problem/C
题目:给定字符串s,t. 给定一个空串z,需要按照规则把z构造成
string z == string t 的字符串。
规则:有限次从s中任取子序列p,然后进行 string z += string p的操作,
s不变。
问能不能构造出p,不能输出-1,可以得话最少几次可以构造出。
大致的想法就是n倍的s字符串串联,然后剔除不必要的字符,就可以得出z,
求最少的倍数n是多少。
主要思路就是用二分去减少时间复杂度,应该是n = 1e5,
<O(n*log(n))。特判-1很简单。然后把s中'a'~'z'存在的字符下标都统计出来,
然后有一个flag表示当前位于s的哪一个位置,我们要充分利用s,所以二分出当前字符最接近flag的位置,
当然要>flag,如果找到了,那就更新flag的位置。如果找不到位置了,说明当前的s已经无用了,需要从新的s中找,
那么ans就要加1,flag就是第一个当前字符出现的位置,然后继续。
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std; const int INF = (int)1e9; struct info{
vector<int> loc[];
string s;
int cnt,flag;
void init(){
for(int i = ; i < ; ++i) loc[i].clear();
cnt = ; flag = -;
}
//统计'a'~'z'的下标
void count(){
int index,l = s.length();
for(int i = ; i < l; ++i){
index = s[i]-'a';
loc[index].push_back(i);
}
}
//特判
bool error(string& x){
int index,l = x.length();
for(int i = ; i < l; ++i){
index = x[i]-'a';
if(loc[index].size()) continue;
return true;
}
return false;
}
void show(){ for(int i = ; i < ; ++i){
if(!loc[i].size()) continue;
cout << char('a'+i) << endl;
for(int j = ; j < loc[i].size(); ++j){
cout << loc[i][j] << ' ';
}cout << endl;
}
}
void work(string& x){ int index,l,r,mid,now;
int len = x.length(); for(int i = ; i < len; ++i){
index = x[i]-'a';
l = ; r = loc[index].size()-;
now = INF;
//二分找答案
while(l <= r){
mid = (l+r) >> ;
if(loc[index][mid] > flag){
now = loc[index][mid];
r = mid - ;
}else l = mid + ;
}
//找到答案,更新flag
if(now != INF) flag = now;
else{//没找到答案,答案加一
//flag为下一个s的该字符出现的第一个位置
flag = loc[index][];
++cnt;
}
}
}
}S; int main(){ int T;
cin >> T;
while(T--){
string t;
cin >> S.s >> t;
S.init();
S.count();
//S.show();
if(S.error(t)){
cout << - << endl;
}else{
S.work(t);
cout << S.cnt << endl;
}
}
} /*
99
aabce
ace
abacaba
aax
ty
yyt
aceaceace
aceaceaceace
aceaceace
acceae */
Educational Codeforces Round 81 (Rated for Div. 2) C. Obtain The String的更多相关文章
- [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)
[Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...
- Educational Codeforces Round 81 (Rated for Div. 2) A-E简要题解
链接:https://codeforces.com/contest/1295 A. Display The Number 贪心思路,尽可能放置更多位,如果n为奇数,消耗3去放置一个7,剩下的放1 AC ...
- Educational Codeforces Round 81 (Rated for Div. 2) B. Infinite Prefixes
题目链接:http://codeforces.com/contest/1295/problem/B 题目:给定由0,1组成的字符串s,长度为n,定义t = sssssss.....一个无限长的字符串. ...
- Educational Codeforces Round 81 (Rated for Div. 2)
A 0~9需要多少笔画,自取7和1,判奇偶 #include<bits/stdc++.h> using namespace std; #define ll long long #defin ...
- Educational Codeforces Round 81 (Rated for Div. 2) 题解
过了n天补的题解:D AB就不用说了 C. Obtain The String 思路挺简单的,就是贪心,但是直接贪心的复杂度是O(|s|*|t|),会超时,所以需要用到序列自动机 虽然名字很高端但是就 ...
- Educational Codeforces Round 81 (Rated for Div. 2)E(线段树)
预处理把左集划分为大小为1~i-1时,把全部元素都移动到右集的代价,记作sum[i]. 然后枚举终态时左集的大小,更新把元素i 留在/移动到 左集的代价. 树状数组/线段树处理区间修改/区间查询 #d ...
- Educational Codeforces Round 81 (Rated for Div. 2) - D. Same GCDs(数学)
题目链接:Same GCDs 题意:给你两个数$a$,$m(1 \leq a < m \leq 10^{10})$,求有多少个$x$满足:$0 \leq x < m$且$gcd(a,m)= ...
- Codeforce |Educational Codeforces Round 77 (Rated for Div. 2) B. Obtain Two Zeroes
B. Obtain Two Zeroes time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 60 (Rated for Div. 2) E. Decypher the String
题目大意:这是一道交互题.给你一个长度为n的字符串,这个字符串是经过规则变换的,题目不告诉你变换规则,但是允许你提问3次:每次提问你给出一个长度为n的字符串,程序会返回按变换规则变换后的字符串,提问3 ...
随机推荐
- mac下搭建http服务器(apache+php),使用homebrew升级php
新版mac依旧预装了 Apache ,但是已经不能在 「系统偏好设置」中的「Web 共享」来开启了,需要手动通过命令行开启. 启动Apache 启动:sudo apachectl start 停止:s ...
- Cesium案例解析(四)——3DModels模型加载
目录 1. 概述 2. 代码 3. 解析 4. 参考 1. 概述 Cesium自带的3D Models示例,展示了如何加载glTF格式三维模型数据.glTF是为WebGL量身定制的数据格式,在网络环境 ...
- 第四次oo博客作业
(1)本单元是撰写UML数据分析器,架构大致如下,在指导书要求的函数外,对于UmlClass类,Umlinterface类,以及状态机,顺序图这四个类重现构造一个类,这个类里有他们所需要的全部信息,另 ...
- JSP其余内置对象及四大范围对象的使用
一.application String getContextPath():获取虚拟路径String getRealPath():获取虚拟路径对应的绝对路径 实例 application.jsp &l ...
- 2python脚本在window编辑后linux不能执行的问题
参考简书博主天道酬勤abcd python脚本在windows编辑后,在linux下执行提示 /usr/bin/python^M: bad interpreter: No such file or d ...
- SpringCloud之eureka注册中心入门
eureka注册中心 一.基本概念 SpringCloud封装 了Netflix公司的eureka作为自己微服务的注册中心.这个注册中心和dubbo中的zookeeper很相似,简单来说,只要你可以将 ...
- Docker 安装 ELK
安装 首先安装 Docker 与 Docker-Compose 相关的组件,我们这里直接使用准备好的 ELK 镜像,执行以下命令从 Dockerhub 上拉取指定版本的镜像,在本例当中我使用的是 7. ...
- 小白的linux笔记4:几种共享文件方式的速度测试——SFTP(SSH)/FTP/SMB
测试一下各个协议的速度,用一个7205M的centos的ISO文件上传下载.5Gwifi连接时,本地SSD(Y7000)对服务器的HDD: smb download 23M/s(资源管理器) smb ...
- git 流程
1.git clone 拉取代码2.git checkout -b '分支名称' 命令意思: 创建并切换到当前新建的本地分支. 查看并创建分支,先远端,后本地.3.将本地分支和远端分之关联 git b ...
- ArcGIS Runtime SDK for Android中SimpleFillSymbol.Style样式
SimpleFillSymbol.Style样式枚举共8种: 1.BACKWARD_DIAGONAL 反对角线填充 2.CROSS 交叉线填充 3.DIAGONAL_CROSS 前后对角线填充 4.F ...