题目链接: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的更多相关文章

  1. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)

    [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...

  2. Educational Codeforces Round 81 (Rated for Div. 2) A-E简要题解

    链接:https://codeforces.com/contest/1295 A. Display The Number 贪心思路,尽可能放置更多位,如果n为奇数,消耗3去放置一个7,剩下的放1 AC ...

  3. Educational Codeforces Round 81 (Rated for Div. 2) B. Infinite Prefixes

    题目链接:http://codeforces.com/contest/1295/problem/B 题目:给定由0,1组成的字符串s,长度为n,定义t = sssssss.....一个无限长的字符串. ...

  4. 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 ...

  5. Educational Codeforces Round 81 (Rated for Div. 2) 题解

    过了n天补的题解:D AB就不用说了 C. Obtain The String 思路挺简单的,就是贪心,但是直接贪心的复杂度是O(|s|*|t|),会超时,所以需要用到序列自动机 虽然名字很高端但是就 ...

  6. Educational Codeforces Round 81 (Rated for Div. 2)E(线段树)

    预处理把左集划分为大小为1~i-1时,把全部元素都移动到右集的代价,记作sum[i]. 然后枚举终态时左集的大小,更新把元素i 留在/移动到 左集的代价. 树状数组/线段树处理区间修改/区间查询 #d ...

  7. 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)= ...

  8. 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 ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) E. Decypher the String

    题目大意:这是一道交互题.给你一个长度为n的字符串,这个字符串是经过规则变换的,题目不告诉你变换规则,但是允许你提问3次:每次提问你给出一个长度为n的字符串,程序会返回按变换规则变换后的字符串,提问3 ...

随机推荐

  1. 学习shiro最佳实践,绝对正确

    按照https://blog.csdn.net/qq_34021712/column/info/26947学习,基本能解决shiro一切问题,谢谢该博主

  2. CoreLocation在iOS8上用法的变化

    1.在使用CoreLocation前需要调用如下函数[iOS8专用]: iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法: (1)始终允许访 ...

  3. 用bootstrap来放置天气和图标的位置 自适应

    今天写了个关于天气的页面,他的摆放位置有点难,花了一两个小时用bootstrap来摆放,但是感觉bug很多 所以今天写下自己的心得,放上代码,以后这种就知道怎么写了 <div class=&qu ...

  4. jps jmap 的使用

    第一部分: 源代码: public class ObjectText { int a=0; public static void main(String[] args) { ObjectText ot ...

  5. 多线程笔记 - disruptor

    disruptor 可以理解为一个生产消费的框架. 具体翻译教程: http://ifeve.com/disruptor-getting-started/ 这个框架从数据上看, 是很强大的. 号称1s ...

  6. Git 工作流程和Git分支管理策略

    git-flow 阮一峰大佬写的文章真不错 git-flow, github-flow, gitlab-flow 阮一峰大佬写的文章真不错

  7. codeforces 1301C Ayoub's function

    题目链接:http://codeforces.com/problemset/problem/1301/C 思路: 纯想想了一次,发现one_cnt >= zero_cnt的时候很简单,就是(n) ...

  8. vue router引入路由与路由配置容易犯错的地方与常见的报错与处理报错

    首先npm安装vue-router插件,就不说了其次: 先看下我本地的目录结构吧 第一步:在src目录下新建一个专门存放router的index.js文件里面的内容为: import Vue from ...

  9. Orleans[NET Core 3.1] 学习笔记(四)( 3 )监控Orleans Silo的方式 OrleansDashboard

    简介 Orleans用起来的确很爽,更爽的是咱们有能监控它的工具. OrleansDashboard 这个工具是一个可视化的Silo监控工具,Silo和Grain的活跃状态一目了然,各个接口的响应速度 ...

  10. python学习----文件的操作(2)

    1.文件指针的操作 f=open("yesterday","r",encoding="utf-8") #文件句柄 #文件内指针的操作 pri ...