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

  1. NSOJ10050 Newspaper Headline

    题意:给你一个<10^4的S串和<10^6的T串,通过将S串重复k次,然后将其中一些多余的字母删掉可以获得T串,问k最小是多少,没有的话输出1. 思路:对于每个T串里的字母,我们从左到右扫 ...

  2. AtCoder Beginner Contest 058 ABCD题

    A - ι⊥l Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Three poles st ...

  3. codeforces Gym 100187H H. Mysterious Photos 水题

    H. Mysterious Photos Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...

  4. 【codeforces 757A】Gotta Catch Em' All!

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

随机推荐

  1. Struts2 入门介绍(一)

    一.什么是Struts2 1.Struts2是一个开发框架,应用于JavaEE三层架构中的web层. 2.Struts2框架是在Struts1和webwork基础上发展的一个全新的框架. 3.Stru ...

  2. Hibernate项目环境搭建

    1.首先在eclipse里面新建一个Java工程. 2.在数据库中新建一个数据库(无需创建表,有Hibernate生成). 3.在项目中导入Hibernate所依赖的jar包,该jar包可以在此下载: ...

  3. [Visual studio code 常见问题解决] ——中文乱码、

    中文乱码问题解决 描述:刚开始安装VScode要打开含中文的文件,都会出现乱码,这时只要点击右下方的“UTF-8”改成“GB2312”即可.但每次都改是很麻烦的,所以要使得VScode默认支持GB23 ...

  4. touch-slide-image

    用htmls5+css3实现的在android和ios,以及wekit新版浏览器上实现手指滑动切换图片. https://github.com/navyxie/touch-slide-image

  5. 网络协议-HTTPS

    转载:http://www.renfed.com/2017/02/03/https/ 一.解决问题 HTTPS解决的是中间人攻击 公网:域名解析--IP:所以中间站点抢答返回错误IP 局域网:路由器找 ...

  6. Echarts 修改折线的颜色和折线的点的大小方法

    series: [{ type: 'line', smooth:true,//折点是圆弧状的                            showSymbol: true,          ...

  7. Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)

    一.面向切面编程AOP 目标:让我们可以“专心做事”,避免繁杂重复的功能编码 原理:将复杂的需求分解出不同方面,将公共功能集中解决 *****所谓面向切面编程,是一种通过预编译方式和运行期动态代理实现 ...

  8. unity3d之实现各种滑动效果

    一. 点击滑动页面 新建了一个带mask的prefab,加上代码只需要将图片prefab.按钮prefab和所想添加的图片 拖进去会自动生成按钮,滑动速度可以随意调time,滑动效果用itween实现 ...

  9. IO流之打印流

    打印流的概述(只有输出就是只与数据目的有关,不会抛出IO异常) 打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式. 打印流根据流的分类: l  字节打印流  PrintStream l ...

  10. js权威指南学习笔记(三)语句

    1.声明语句 如果用var声明的变量没有初始化,那么这个变量的值会被初始化为undefined. 函数声明语句的语法如下:       4 4           1 console.log(func ...