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的概 ...
随机推荐
- win10中xshell的ssh链接virtualBox中的centos7
win10下virtualbox中centos7.3与主机通过xshell的ssh建立连接的方法 2017-02-19 01:29 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近 ...
- Java动态代理的理解
代码内容: https://github.com/cjy513203427/Java_Advanced_Knowledge/tree/master/src/com/advance/dynamic_pr ...
- WEB项目构建优化之自动清除CSS中的图片缓存
在web项目构建发布时,经常遇到css中图片的修改优化,那么如何清除图片的缓存成为必须要解决的问题.曾经有过傻傻的方法就是直接在图片后面添加随机数.今天主要是从构建自动化方式来解决这个问题,提高开发及 ...
- disruptor--Introduction
The best way to understand what the Disruptor is, is to compare it to something well understood and ...
- NPM 与前端包管理
我们很清楚,前端资源及其依赖管理一直是 npm 的重度使用场景,同时这也一直是 Node.js 普及的重要推动力.但这类应用场景到底有多重度?这是一个很难回答的问题.这份 “npm 最常下载的包的清单 ...
- 新建文件可选类型插件:SublimeTmpl
介绍:SublimeTmpl,新建文件可选类型.编辑模版在:SublimeTmpl\templates"文件夹修改 1.安装: 通过 Package Control Package Cont ...
- Telerik 控件本地化
一共四种方式: Localization using built-in resources Resource keys Localization using ResourceManager Local ...
- SSRS Report Knowledge Base
1. 获取Textbox的值,根据Textbox值更改单元格颜色 Textbox值:=ReportItems!Textbox1.Value 当前单元格的值:=Me.Value =IIF(ReportI ...
- ASP.NET Visual Studio2010 发布Web网站问题详解
今天研究了一下如何发布web网站,之前总是没耐心,遇到点问题就没心情搞了,今天总算有点耐心搞明白了.其实遇到的问题还是挺多的,网上也没有太全的解释,所以结合自己还有别人的方法进行一下总结. 环境:Wi ...
- 自己玩虚拟机上mongo备份
rs.initiate({_id:"shard1RS",members:[{_id:1,host:"127.0.0.1:27018",priority:2},{ ...