题目链接: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. 使用Vue.prototype在vue中注册和使用全局变量

    在main.js中添加一个变量到Vue.prototype Vue.prototype.$appName = 'My App' 这样 $appName 就在所有的 Vue 实例中可用了,甚至在实例被创 ...

  2. C++泛化单链表

    泛型单链表 单链表将每个数据分为节点,每个节点存储数据和指向下一个节点的指针.这样数据就不用在内存中使用连续的存储空间,有更大的灵活性. 这里将单链表分为节点类(Node)和链表类(singleLin ...

  3. 11种常用css样式之表格和定位样式学习

    table表格中border-collapse: collapse;/*表格边框是否合并*/border-spacing: 10px;/*表格边框之间的距离*/定位详情可以阅读position属性值4 ...

  4. 给test函数加个装饰器!

    import timedef timer(func): def deco(*args,**kwargs): start_time=time.time() func(*args,**kwargs) st ...

  5. The Ether 靶场

    0x01 首先对靶场进行端口扫描 发现只开启了80端口和22端口 0x02 目录扫描 访问了几个目录并没有什么发现 0x03 访问主页几个网站链接 发现了一个疑似文件包含的漏洞 0x04 抓包进行分析 ...

  6. java-十进制与十六进制的转化

    问题: 在一些特定的情况下,程序中需要用到进制之间的转化,现在来说说十进制和十六进制的转化. 其实java进制转换非常的简单. 那问什么还要说这个问题呢? 因为在转化的时候遇到一个问题... 记录一下 ...

  7. ARC 064 F-Rotated Palindromes

    题意 问有多少个长度为 \(N\) 且字符集大小为 \(K\) 的字符串可以通过回文串旋转 (把第一个字符移到最后)若干次得到.\(N,K\le 10^9\) 做法 设\(f_i\)为最小周期为\(i ...

  8. 185.nvm和node.js环境配置

    安装nvm nvm(Node Version Manager)是一个用来管理node版本的工具,我们之所以使用node,是因为我们需要使用node中的npm(Node Package Manager) ...

  9. 数据结构(集合)学习之Map(二)

    集合 框架关系图 补充:HashTable父类是Dictionary,不是AbstractMap. 一:HashMap中的链循环: 一般来说HashMap中的链循环会发生在多线程操作时(虽然HashM ...

  10. hibernate报错:MappingException: Could not determine type for...解决办法

    有时候实体里的一些属性并不想映射到数据库(比方说子级菜单List), 如果不做处理的话会报字段映射错误找不到这列Column Not Found 例如:org.hibernate.MappingExc ...