【Educational Codeforces Round 81 (Rated for Div. 2) C】Obtain The String

题目链接

【题解】

显然我们得按顺序获得目标t的每个字符。
对于t[0],t[1],t[2],t[3]...
我们可以在s中找到这么一个子序列。
显然如果找到尾巴还是没有需要的t[i]。
就得从s[0]开始重新开始顺序找。
(然后答案递增,因为表示要重新开始加一个子序列了)
但是如果这么直接找的话,复杂度是O(|s||t|)的
是无法接受的。
所以我们可以另外开一个数组
next[i][j]
表示在i-1位置,如果要找下一个紧接着的j的话,需要到哪个位置找。(这个next数组整体往左偏移了下,便于处理,因为不能包括本身,它的含义是不包括自己的下一个)
这个数组能在O(26
|s|)的时间复杂度内处理出来。
这样,在遍历t的每个字符的时候,就能在O(1)的时间内找到t[i]在s中的位置了。
所以算答案的时候复杂度是O(|t|)级别。

【代码】

#include <bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 1e5;

int n,x;
string s;
string t;
int _next[N+10][30];

int main(){
    #ifdef LOCAL_DEFINE
        freopen("E:\\rush.txt","r",stdin);
    #endif // LOCAL_DEFINE
    ios::sync_with_stdio(0),cin.tie(0);
    int T;
    cin >> T;
    while (T--){
        cin >> s;
        cin >> t;
        int len = s.size();
        for (int i = 0;i<26;i++) _next[len][i] = -1;
        for (int i = len-1;i>=0;i--){
            for (int j = 0;j < 26;j++) _next[i][j] = _next[i+1][j];
            int key = s[i]-'a';
            _next[i][key] = i+1;
        }
        //cout<<_next[0][0]<<endl;
        len = t.size();
        int ans = 1;
        int cur = 0;
        bool ok = true;
        for (int i = 0;i < len;i++){
            int kk = t[i]-'a';
            if (_next[cur][kk]!=-1){
                cur = _next[cur][kk];
            }else{
                ans++;
                cur = 0;
                if (_next[0][kk]==-1){
                    ok = false;
                    break;
                }
                cur = _next[cur][kk];
            }
            //cout<<cur<<endl;
        }
        if (!ok) cout<<-1<<endl;else
        cout<<ans<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/AWCXV/p/12242093.html

时间: 2024-07-30 23:35:43

【Educational Codeforces Round 81 (Rated for Div. 2) C】Obtain The String的相关文章

【Educational Codeforces Round 81 (Rated for Div. 2) A】Display The Number

题目链接 [题解] 优先用2个棒子来凑1. 如果为奇数的话,多出来一根用3根来凑个7放在开头 [代码] #include <bits/stdc++.h> using namespace std; int main(){ #ifdef LOCAL_DEFINE freopen("E:\\rush.txt","r",stdin); #endif // LOCAL_DEFINE ios::sync_with_stdio(0),cin.tie(0); int T

【Educational Codeforces Round 81 (Rated for Div. 2) B】Infinite Prefixes

题目链接 [题解] 把0看成是1,把1看成是-1 求一个前缀和. pre[i] = pre[i-1]+1 得到delta = pre[n] 显然对于每个位置的值pre[i] 再复制一遍s的话. 下一个s的该位置,也即i+n的前缀和显然为pre[i]+delata 那么无限的情况就很显然了. 即pre[i]==x,而且delata==0. 只要出现一个这种情况,就是无限. 其他情况,每个位置都会朝着目标远离或者毕竟. 看看差值不是不是delta的整数倍就可以了. 如果是的话,说明可以变成x [代码

Educational Codeforces Round 81 (Rated for Div. 2) C. Obtain The String

题目链接:http://codeforces.com/contest/1295/problem/C 题目:给定字符串s,t.  给定一个空串z,需要按照规则把z构造成 string z == string t 的字符串. 规则:有限次从s中任取子序列p,然后进行 string z += string p的操作, s不变. 问能不能构造出p,不能输出-1,可以得话最少几次可以构造出. 大致的想法就是n倍的s字符串串联,然后剔除不必要的字符,就可以得出z, 求最少的倍数n是多少. 主要思路就是用二分

Educational Codeforces Round 81 (Rated for Div. 2) B. Infinite Prefixes

题目链接:http://codeforces.com/contest/1295/problem/B 题目:给定由0,1组成的字符串s,长度为n,定义t = sssssss.....一个无限长的字符串. 题目定义一个平衡值x,取t的任意前缀Q,如果Q满足cnt(0,q) - cnt(1,q) = x,即Q中0 的个数-1的个数= x,说明当前的Q是满足题意得一个前缀,问满足x的前缀有多少个, 如果x = ∞,则输出-1. input 6 10 010010 题目给定说q的长度为28,30,32是平

Educational Codeforces Round 81 (Rated for Div. 2)

A - Display The Number 题意:给n根火柴,拼出最大的数字. 题解:肯定是数字越多越大,所以尽可能多拿最便宜的2根火柴一个"1",多余的肯定是拿一个"7",由于n>=2,没有特例. void test_case() { int n; scanf("%d", &n); if(n % 2 == 1) { printf("%d", 7); n -= 3; } while(n) { printf(&q

Educational Codeforces Round 81 (Rated for Div. 2) A Display The Number

A. Display The Number time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed

Educational Codeforces Round 81 (Rated for Div. 2)F(线段树)

预处理把左集划分为大小为1~i-1时,把全部元素都移动到右集的代价,记作sum[i]. 然后枚举终态时左集的大小,更新把元素i 留在/移动到 左集的代价. 树状数组/线段树处理区间修改/区间查询 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 #define ll long long 5 const int N=2e5+7; 6 struct Tree{ 7 ll minn,la

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is