Strings of Impurity(序列自动机)

Strings of Impurity(序列自动机)

题目描述
Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.
·Let s′ be the concatenation of 10 100 copies of s. t is a subsequence of the string s′1s′2…s′i (the first i characters in s′).
Notes
·A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of ‘contest’ include ‘net’, ‘c’, and ‘contest’.
Constraints
·1≤|s|≤10 5
·1≤|t|≤10 5
·s and t consists of lowercase English letters.
输入
Input is given from Standard Input in the following format:

s
t

输出
If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.
样例输入 Copy
【样例1】
contest
son
【样例2】
contest
programming
【样例3】
contest
sentence
样例输出 Copy
【样例1】
10
【样例2】
-1
【样例3】
33
提示
样例1解释
t= ‘son’ is a subsequence of the string ‘contestcon’ (the first 10 characters in s′= ‘contestcontestcontest…’), so i=10 satisfies the condition.On the other hand, t is not a subsequence of the string ‘contestco’ (the first 9 characters in s′), so i=9 does not satisfy the condition.

Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10.
样例2解释
t= ‘programming’ is not a substring of s′= ‘contestcontestcontest….’ Thus, there is no integer i satisfying the condition.

样例3解释
Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.

题意:求字符串t在字符串s循环出现,第几个字符可以出现完。
#pragma GCC optimize(3 , "Ofast" , "inline")
 
#include <bits/stdc++.h>
 
#define rep(i , a , b) for(register int i=(a);i<=(b);i++)
#define rop(i , a , b) for(register ll i=(a);i<(b);i++)
#define per(i , a , b) for(register ll i=(a);i>=(b);i--)
#define por(i , a , b) for(register ll i=(a);i>(b);i--)
 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll , ll> pi;
const int maxn = 1e5+10;
 
char s[maxn];
char t[maxn];
bool vis[30];
int pos[30];
int mp[30][maxn];
 
template<class T>
inline void read (T &x) {
    x = 0;
    int sign = 1;
    char c = getchar ();
    while (c < '0' || c > '9') {
        if ( c == '-' ) sign = - 1;
        c = getchar ();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar ();
    }
    x = x * sign;
}
 
 
int main () {
    cin>>s+1;
    cin>>t+1;
    int ls = strlen (s+1);
    int lt = strlen (t+1);
    rep (i,1,ls) {
        vis[s[i]-'a']=1;
    }
    rep(i,0,25) mp[i][ls+1] = -1;
    per (i,ls,1){
        rep (j,0,25){
            mp[j][i] = mp[j][i+1];
        }
        mp[s[i]-'a'][i] = i;
    }
    rep (i,1,lt) {
        if(!vis[t[i]-'a']) {
            cout<<-1<<endl;
            return 0;
        }
    }
    int now = 1;
    ll ans = 0;
    for(int i=1;i<=lt;i++) {
        if( mp[t[i]-'a'][now] != -1 ){
            ans=ans+mp[t[i]-'a'][now]-now+1;
            now = mp[t[i]-'a'][now]+1;
        }
        else{
            ans=ans+ls-now+1;
            now = 1;
            i--;
        }
    }
    cout<<ans<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值