CodeForces 91A Newspaper Headline
题目链接:CodeForces - 91A Newspaper Headline
官方题解:
In this problem letters from s1 should be taken greedily: take the left letter from the right of the last used letter, if there is no necessary letter from the right of the right used letter the the search should be started from the beginning of string s1 and the answer should be increased by one. But the brute solution get TL and have complexity O(Ans * |s1|). 
This solution can be optimized using the following way. For every position in s1 let's precalculate positions of the closest letters from the right of it from the alphabet. It can be done by moving from the right to the left ins s1 and remembering the last position of every type of symbol. This solution have complexity O(|s1| * K + |s2|), where K is a size of alphabet.
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<cmath>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std; const int N = ;
const int M = 1e6+; char s1[N], s2[M];
int d[N][];
bool vis[]; int main () {
scanf("%s %s", s1, s2);
int i, j;
CLR(d, -);
CLR(vis, );
int len1 = strlen(s1);
int len2 = strlen(s2);
for(i = ; i < len1; ++i) {
vis[ s1[i]-'a' ] = ;
for(j = ; j <= i; ++j) {
if(d[j][ s1[i]-'a' ] == -)
d[j][ s1[i]-'a' ] = i;
}
}
for(j = ; j < len2; ++j) {
if(vis[ s2[j]-'a' ] == ) {
printf("-1\n"); return ;
}
}
int ans = ;
for(j = ; j < len2; ) {
i = ;
while(i < len1 && j < len2 && d[i][ s2[j]-'a' ] != -) {
i = d[i][ s2[j]-'a' ] + ;
j++;
}
ans++;
}
printf("%d\n", ans);
return ;
}
CodeForces 91A Newspaper Headline的更多相关文章
- NSOJ10050 Newspaper Headline
		
题意:给你一个<10^4的S串和<10^6的T串,通过将S串重复k次,然后将其中一些多余的字母删掉可以获得T串,问k最小是多少,没有的话输出1. 思路:对于每个T串里的字母,我们从左到右扫 ...
 - AtCoder Beginner Contest 058 ABCD题
		
A - ι⊥l Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Three poles st ...
 - codeforces Gym 100187H H. Mysterious Photos 水题
		
H. Mysterious Photos Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...
 - 【codeforces 757A】Gotta Catch Em' All!
		
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
 - python爬虫学习(5) —— 扒一下codeforces题面
		
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
 - 【Codeforces 738D】Sea Battle(贪心)
		
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
 - 【Codeforces 738C】Road to Cinema
		
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
 - 【Codeforces 738A】Interview with Oleg
		
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
 - CodeForces - 662A Gambling Nim
		
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
 
随机推荐
- TCP连接管理(TCP Connection Management)
			
在最近的求职面试过程中,关于"建立TCP连接的三次握手"不止一次被问到了,虽然我以前用同样的问题面试过别人,但感觉还是不能给面试官一个很清晰的回答.本文算是对整个TCP连接管理做一 ...
 - 阿里云服务器docker搞定镜像
			
docker的安装可以看前面的文章 这里我的docker已经安装完成了,该搞镜像了:这里的镜像用的是我以前自己搞的,虽然镜像有点大,但是胜在自己搞的,熟悉(熟悉不熟悉鬼知道) 我的镜像放在了阿里云容器 ...
 - Magento 2开发教程 - 如何添加新产品属性
			
添加产品属性是一种在Magento 1 和 Magento 2最受欢迎的业务. 属性是解决许多与产品相关的实际任务的有力方法. 这是一个相当广泛的话题,但在这个视频中,我们将讨论添加一个下拉类型属性到 ...
 - C# 连接Oracle,进行查询,插入操作
			
注:OracleConnection和OracleCommand已被标注为[弃用的],可以使用System.Data.OleDb.OleDbConnection代替OracleCOnnection,使 ...
 - android socket 通讯(客户端) 发送数据
			
/** ClientSocket通讯类 **/ public class ClientSocket { /**服务器地址*/ private String serverUrl=&q ...
 - Java - 如何进行安全发布
			
首先让我简单解释一下所谓"发布". 发布(publish),使对象可以在当前作用域之外的代码中可见,如果该对象被发布,则该对象的非私有域中引用的所有实例同样也会被发布. 不仅仅是作 ...
 - .NET的EF框架中:在应用程序配置文件中找不到名为“”的连接字符串问题
			
今天在使用EF Code First框架时,当把模型都定义好了,想通过程序包管理控制台利用enable-migrations –force来生成数据库表的时候报错了,如下: 找不到连接字符串,但是我仔 ...
 - 自己实现async和await
			
无意当中看了一些博文,说有人想自己尝试实现基于异步操作的方法: 1)直接使用Task(不说咯,这个是微软给我们的标准实现方法). 2)必须继承INotifyCompletion接口,同时自己实现IsC ...
 - Java基础(9)——数组
			
难点儿的已经过去啦,现在又开始基础了哈~ 之前讲变量的时候,变量是一个个的呀~,那我要搞一串变量该啷个办呢?Java给我们出了个好东西叫数组(*^▽^*) 数组呢,就是将变量一组一组的存起来,这个也是 ...
 - swagger2配置和使用
			
1.导入swagger2 <dependency> <groupId>io.springfox</groupId> <artifactId>spring ...