题目链接: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. ab使用详解—如何使用apache性能测试工具进行压力测试

    作为后端工程师,除了实现业务需求之外,需要考虑的就是自己写的服务,在大并发下是否能正常运行了.但是,在一般开发情况下,没那么多大并发情况让你测试,那该怎么办呢? 这时候,我们就可以用到apache的压 ...

  2. vue路由--动态路由

    前面介绍的路由都是路径和组件一对一映射的 有时候需要多个路径映射到一个组件,这个组件根据参数的不同动态改变,这时候需要用到动态路由 动态路由这样定义路由路径: path: '/foo/:id'--可以 ...

  3. 【转】Redis内部数据结构详解 -- skiplist

    本文是<Redis内部数据结构详解>系列的第六篇.在本文中,我们围绕一个Redis的内部数据结构--skiplist展开讨论. Redis里面使用skiplist是为了实现sorted s ...

  4. leetcode--js--Median of Two Sorted Arrays

     问题描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of ...

  5. bootstrap--font-face问题

    在做仿天猫项目时,预期 已经用cdn加载bootstrap,但是不显示文字图标:实际情况: 发现是引用bootstrap的版本导致的.4.0.0版本及以后的css源码中找不到@font-face.可以 ...

  6. C#设置自定义文件图标实现双击启动

    修改注册表,双击文件直接打开 string strProject = "Exec"; string p_FileTypeName =".cdb";//文件后缀 ...

  7. 少量代码设计一个登录界面(二) – .NET CORE(C#) WPF开发

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. 阅读导航 本文背景 代码实现 本文参考 源码 1. 本文背景 同上篇文章<少量代码设计一 ...

  8. 聊聊GIS数据的四个分层与GIS服务

    本篇不讨论矢量栅格数据的结构,也不讨论矢量与栅格的区别(即设定读者有这方面的基础). 版权声明:原创.博客园/B站/小专栏/知乎/CSDN @秋意正寒 转载请标注原地址并声明转载: https://w ...

  9. 剑指offer-面试题63-股票的最大利润-数组

    /* 题目: 给定一个股价序列,求一次交易的最大利润. */ #include<iostream> #include<vector> using namespace std; ...

  10. 软件模拟spi的注意事项

    前几天遇到了软件模拟spi的时候,读和写不一致的现象,后来仔细研究了一下,其实是时序性问题不对. spi的有四种时序,硬件实现的时候,很简单,初始化后直接调用api即可.但是软件模拟就比较麻烦. 举例 ...