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 ...
随机推荐
- 【STM32H7教程】第61章 STM32H7的MDMA基础知识和HAL库API
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第61章 STM32H7的MDMA基础知识和HAL ...
- dotnetcore3.1 WPF 实现多语言
dotnetcore3.1 WPF 实现多语言 Intro 最近把 DbTool 从 WinForm 迁移到了 WPF,并更新到了 dotnet core 3.1,并实现了基于 Microsoft.E ...
- HTML连载70-相片墙、盒子阴影和文字阴影
一. 制作一个相片墙 二. <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- redis的基础知识
select切换数据库 remoteSelf:0>select 0 "OK" dbsize查看当前数据库的key数量 remoteSelf:0>dbsize " ...
- 1、SSH无密码访问
1.在需要无密码登录远程服务器的机器上(如A→B服务器)生成密码对 A:服务器操作: ssh-keygen -t rsa :输出的内容直接一路回车即可(enter) 执行上面一步,会在~/.ssh目录 ...
- ELK学习001:Elastic Stack简介
ELK简介: ELK Stack:ELK是Elasticsearch.Logstash.Kibana的缩写简称,这三者都是开源软件.ELK是5.0版本前的统称:这是一套统一的日志收集分析系统.它能够方 ...
- MFC/QT 学习笔记(三)——MFC模板创建
新建项目->MFC模板->MFC应用程序->应用程序类型:单个文档:项目样式:MFC 标准->下一步...OK 此时点击运行,可直接弹出窗口. 调整 视图->类视图: · ...
- node.remove()
node.remove()方法是在DOM 4规范中实现的,由于糟糕的浏览器支持,不建议使用.应该使用removeChild方法,但是该方法必须要清楚父元素,通常的做法是:node.parentNode ...
- 为什么你SQL Server中SQL日期转换出错了呢?
开发人员有时候使用类似下面SQL将字符串转换为日期时间类型,乍一看,这样的SQL的写法是没有什么问题的.但是这样的SQL其实有时候就是一个定时炸弹,随时可能出现问题(),下面简单对这种情况进行一个简单 ...
- HTML速查
HTML 基本文档 <!DOCTYPE html> <html> <head> <title>文档标题</title> </head& ...