Codeforces 91A-Newspaper Headline

本文探讨了一种算法问题,即如何通过拼接特定数量的原始字符串并从中删除部分字符来形成目标字符串。该问题涉及字符串操作、字符匹配等关键技术,并提供了一个具体的实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Newspaper Headline
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A newspaper is published in Walrusland. Its heading is s1, 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 s2. 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 s1 will Fangy need to glue them, erase several letters and get word s2?

Input

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).

Output

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.

Examples
input
abc
xyz
output
-1
input
abcd
dabc
output
2

题意:给定一个A串,一个B串,用不同的去除某些位置的A串组成B串,问最少需要几串?

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>

using namespace std;

char s1[10009],s2[1000009];
int a[10009][26],b[26];

int main()
{
    while(~scanf("%s %s",s1,s2))
    {
        memset(a,-1,sizeof(a));
        memset(b,0,sizeof b);
        int sum=0,flag=0;
        int len1=strlen(s1),len2=strlen(s2);
        for(int i=0; i<len1; i++)
        {
            b[s1[i]-'a']=1;
            for (int j=0; j<=i; j++)
                if (a[j][s1[i]-'a']==-1) a[j][s1[i]-'a']=i;
        }
        for (int i=0; i<len2; i++)
            if (!b[s2[i]-'a'])
            {
                printf("-1\n");
                flag=1;break;
            }
        if(flag) continue;
        int j=0;
        while(j<len2)
        {
            sum++;
            int i=0;
            while(j<len2&&i<len1&&a[i][s2[j]-'a']!=-1)
            {
                i=a[i][s2[j]-'a']+1;
                j++;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}
### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值