zoukankan      html  css  js  c++  java
  • CodeForces 91A Newspaper Headline

    题目描述

    A newspaper is published in Walrusland. Its heading is s 1 , it consists of lowercase Latin letters. Fangy the little walrus wants to buy several such newspapers, cut out their headings, glue them one to another in order to get one big string. After that walrus erase several letters from this string in order to get a new word s 2 . It is considered that when Fangy erases some letter, there's no whitespace formed instead of the letter. That is, the string remains unbroken and it still only consists of lowercase Latin letters.

    For example, the heading is "abc". If we take two such headings and glue them one to the other one, we get "abcabc". If we erase the letters on positions 1 and 5, we get a word "bcac".

    Which least number of newspaper headings s 1 will Fangy need to glue them, erase several letters and get word s 2 ?

    输入描述:

    The input data contain two lines. The first line contain the heading s1, the second line contains the word s2. The lines only consist of lowercase Latin letters (1 ≤ |s1| ≤ 104, 1 ≤ |s2| ≤ 106).

    输出描述:

    If it is impossible to get the word s2 in the above-described manner, print "-1" (without the quotes). Otherwise, print the least number of newspaper headings s1, which Fangy will need to receive the word s2.

    示例1

    输入

    复制
    abc
    xyz
    abcd
    dabc

    输出

    复制
    -1
    2

    题意:给出两个字符串s1,s2,问需要几个s1字符串才能得到s2的序列(可以是不连续的)

    #include<iostream>
    #include<algorithm>
    #include<math.h>
    #include<string.h>
    #include<set>
    #define ll long long
    #define M 0x3f3f3f3f3f
    using namespace std;
    string s1,s2;
    set<int>s[26];
    int main()
    {
        while(cin>>s1>>s2)
        {
            for(int i=0;i<26;i++)
                s[i].clear();
            int flag=0,cnt=1,last=-1;
            for(int i=0;s1[i];i++)
                s[s1[i]-'a'].insert(i);
            for(int i=0;s2[i];i++)
            {
                if(s[s2[i]-'a'].size()==0)
                {
                    flag=1;
                    break;
                }
                else
                {
                    set<int>::iterator it=s[s2[i]-'a'].upper_bound(last);
                    if(it==s[s2[i]-'a'].end())
                    {
                        cnt++;
                        last=-1;
                    }
                    last=*s[s2[i]-'a'].upper_bound(last);
                }
            }
            if(flag==1)
                cout<<-1<<endl;
            else
                cout<<cnt<<endl;
            
        }
        return 0;
    }
  • 相关阅读:
    大数据集群迁移的那一夜是怎么过的
    什么是Lambda架构
    从0到1搭建自助分析平台
    业务重要?还是技术重要?
    如何从0到1搭建大数据平台
    从0到1搭建大数据平台之数据采集系统
    高频面试题:秒杀场景设计
    面试官:面对千万级、亿级流量怎么处理?
    来自朋友最近阿里、腾讯、美团等P7岗位面试题
    《我想进大厂》之JVM夺命连环10问
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11768072.html
Copyright © 2011-2022 走看看