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.
·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.
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;
}