意:

求最少需要几个s1串拼接存在子串s2 (1≤|s1|≤1e4,1≤|s2|≤1e6).

思路(感谢ZQC):

每个字母的出现位置存个vector.

假设你当前已经用了A串的前x个字符,现在想要匹配一个'x',那你就在'x'的vector那里二分出第一个大于x的位置,

如果匹配不到的话,就相当于需要一个新的串。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL; vector<int>xs[30];
char s1[10010],s2[1000010];
map<int,int>mp; int main()
{
    int x,n,m,pos,ans,k;
    vector<int>::iterator y;
    scanf("%s%s",s1,s2);
    n=strlen(s1);
    for(int i=0;i<n;i++)
    {
        x=s1[i]-'a';
        xs[x].push_back(i);
        mp[x]=1;
    }
    m=strlen(s2);
    pos=-1;
    ans=1;
    for(int i=0;i<m;i++)
    {
        x=s2[i]-'a';
        if(!mp[x])
        {
            puts("-1");
            return 0;
        }
        y=upper_bound(xs[x].begin(),xs[x].end(),pos);
        if(y==xs[x].end())
        {
            ans++;
            y=xs[x].begin();
        }
        k=y-xs[x].begin();
        pos=xs[x][k];
    }
    printf("%d\n",ans);
    return 0;
}

Codeforces 92C【二分】的更多相关文章

  1. CodeForces - 363D --二分和贪心

    题目:CodeForces - 363D 题意:给定n个学生,其中每个学生都有各自的私己钱,并且自己的私己钱只能用在自己买自行车,不能给别人. 给定m个自行车,每个自行车都有一个价格. 给定公有财产a ...

  2. Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论

    n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...

  3. Codeforces 732D [二分 ][贪心]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...

  4. codeforces 359D 二分答案+RMQ

    上学期刷过裸的RMQ模板题,不过那时候一直不理解>_< 其实RMQ很简单: 设f[i][j]表示从i开始的,长度为2^j的一段元素中的最小值or最大值 那么f[i][j]=min/max{ ...

  5. CodeForces - 551C 二分+贪心

    题意:有n个箱子形成的堆,现在有m个学生,每个学生每一秒可以有两种操作: 1: 向右移动一格 2: 移除当前位置的一个箱子 求移除所有箱子需要的最短时间.注意:所有学生可以同时行动. 思路:二分时间, ...

  6. Robin Hood CodeForces - 672D (二分)

    大意: 给定数组$a$, 每次操作使最大元素减小1最小元素增大1, 求k次操作后最大值与最小值的差. 二分出k次操作后最大值的最小值以及最小值的最大值, 若和能平分答案即为$max(0,R-L)$, ...

  7. Queries about less or equal elements CodeForces - 600B(二分)

    You are given two arrays of integers a and b. For each element of the second arraybj you should find ...

  8. Enduring Exodus CodeForces - 655C (二分)

    链接 大意: n个房间, 1为占用, 0为未占用, John要将k头奶牛和自己分进k+1个空房间, 求John距最远的奶牛距离的最小值 这种简单题卡了20min.... 显然对于固定的k+1个房间, ...

  9. CodeForces 483B 二分答案

    题目: B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input s ...

随机推荐

  1. python 面试题(一)

    1 Python的函数参数传递 看两个例子: Python   1 2 3 4 5 a = 1 def fun(a):     a = 2 fun(a) print a  # 1   Python   ...

  2. 理解SetCapture()和ReleaseCapture()及GetCapture()作用

    正常情况下,鼠标指针位于哪个窗口区域内,鼠标消息就自动发给哪个窗口.如果调用了SetCapture,之后无论鼠标的位置在哪,鼠标消息都发给指定的这个窗口,直到调用ReleaseCapture或者调用S ...

  3. Mysql备份脚本python编写

    #!/usr/bin/env python #-*- coding: UTF-8 -*- ####################################################### ...

  4. 1103 Integer Factorization (30)(30 分)

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  5. sqlite表结构动态读取工具(Chole ORM框架)

    Chole ORM框架 sqlIte于嵌入式数据库读取比较有利,不需要安装office也可以进行,可以在服务器系统当中使用. 所以我开发了这款工具,然后就是为了动态的读取表结构,然后根据表结构加载所有 ...

  6. ACM学习历程—HDU 5451 Best Solver(Fibonacci数列 && 快速幂)(2015沈阳网赛1002题)

    Problem Description The so-called best problem solver can easily solve this problem, with his/her ch ...

  7. tcpdump示例

    今天有需求要用tcpdump,给一个我使用的例子: sudo /usr/sbin/tcpdump  dst 10.20.137.24 and tcp port 8080 -A -s0  -w nous ...

  8. C#添加修改控件css样式

    一.添加属性 MyStyleSheet.Attributes.Add("href","/css/flostyle.css") 二.改变css样式 if (use ...

  9. mysql基础之三:char与varchar区别,varchar(M)能存多少

    char与varchar区别 char (13)长度固定, 如'1234567890' 存储需要空间 10个字符; varchar(13) 可变长 如'1234567890' 需要存储空间 11字符; ...

  10. C#动态给EXCEL列添加下拉选项

    Microsoft.Office.Interop.Excel.Application excel=new Microsoft.Office.Interop.Excel.Application(); M ...