zoukankan      html  css  js  c++  java
  • Codeforces 1295C Obtain The String (二分)

    传送门

    题意:

    给两个字符串(s,t)
    (z)为一个空串
    问对(s)进行多少操作能将(z)变成(t)
    每次操作为,选s的一些字串(可以不连续,但顺序不能乱),让把这个字串加到Z的后面
    输出最少操作的次数,如果(z)不能变成(t),输出-1

    思路:

    把字符串放入vector,或者二维数组里,因为只有小写字母,30个空间已足够
    如果t的字符在s中找不到,那肯定输出-1
    如果能找到为了使操作数最少,肯定用比较靠前且满足题意的
    找位置的时候二分即可用upper_bound,upper_bound的用法

    代码:

    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    #include <vector>
    #include <math.h>
    #include <map>
    #include <queue>
    #include <set>
    using namespace std;
    typedef long long ll;
    const int maxn=1e5+50;
    char s[maxn],t[maxn];
    
    int main()
    {
        int q;
        scanf("%d",&q);
        while(q--){
    
            scanf("%s %s",s,t);
            vector<int>p[30];
            for(int i=0;s[i];i++){
                p[s[i]-'a'].push_back(i);
            }
            int ans=1,flag=1;
            int pos,ppos=-1;//pos为当前正在找的字符k在p[k]中的位置,ppos当前在s串中的位置
            for(int i=0;t[i];i++){
                int k=t[i]-'a';
                if(p[k].size()==0){//在t中找不到
                    flag=0;
                    break;
                }
                pos=upper_bound(p[k].begin(),p[k].end(),ppos)-p[k].begin();//ppos当前在s串中的位置,看在p[k]的哪个位置,且能不能满足
                if(pos==p[k].size()){//如果字符为k的找到头了,说明需要从头再找,ans++
                    ans++;
                    pos=0;
                }
                ppos=p[k][pos];
            }
            if(!flag)printf("-1
    ");
            else printf("%d
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    数据增强
    变态跳台阶
    跳台阶
    数据分析--简单回测框架开发
    数据分析--羊驼交易法则(选股)
    数据分析--动量策略vs反转策略(选股)
    数据分析--PEG策略(选股)
    数据分析--布林带策略(择时)
    数据分析--均值回归策略(选股)
    数据分析--单因子选股策略、多因子选股策略(选股)
  • 原文地址:https://www.cnblogs.com/zzl_Alexander/p/12250640.html
Copyright © 2011-2022 走看看