91A - Newspaper Headline - CodeForces Solution


greedy strings *1500

Please click on ads to support us..

Python Code:

import copy

if __name__ == "__main__":
    s, t = str(input()), str(input())
    n = len(s)
    pos = [0] * 26
    for i in range(26):
        pos[i] = n
    nxt = [[0] * 26] * n
    for k in range(2):
        for i in range(n-1, -1, -1):
            nxt[i] = copy.deepcopy(pos)
            pos[ord(s[i]) - ord("a")] = i

    ans, i = 1, 0
    for j in range(1 if s[0] == t[0] else 0, len(t), 1):
        next = nxt[i][ord(t[j]) - ord("a")]
        if next == n:
            print(-1)
            exit()
        if next <= i:
            ans += 1
        i = next
    print(ans)

C++ Code:

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 1000000007;
double eps = 1e-12;
#define ln "\n"
#define printVector(a) for(int i=0; i<a.size(); i++){cout<<a[i]<<" ";}cout<<ln;
#define print_array(a,n) for(int i=0; i<n; i++){cout<<a[i]<<" ";}cout<<ln;
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define take_vector(a) for(auto &x:a)cin>>x;
#define take_array(a,n) for(int i=0;i<n;i++){cin>>a[i];}
#define take_matrix(a,m,n) for(int i=0; i<m; i++){for(int j=0; j<n; j++){cin>>a[i][j];}}
#define print_matrix(a,m,n) for(int i=0; i<m; i++){for(int j=0; j<n; j++){cout<<a[i][j]<<" ";}cout<<ln;}
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a/gcd(a,b))*b
#define count(a,x) count(a.begin(), a.end(),x)
#define sum(a) accumulate(a.begin(), a.end(),0)
#define max_ele(a) *max_element(a.begin(), a.end())
#define min_ele(a) *min_element(a.begin(), a.end())
void solve() {
	string s, t;
	cin >> s >> t;
	int n = s.size();
	int m = t.size();
	set<int> pos[26];
	for(int i = 0; i<n; i++) {
		pos[s[i]-'a'].insert(i);
	}
	int ans = 1, lastIndex = -1;
	for(int i= 0; i<m; i++) {
		int ch = t[i]-'a';
		if(pos[ch].size() ==  0) {
			cout << -1 << ln;
			return; 
		}
		auto it = pos[ch].upper_bound(lastIndex);
		if(it == pos[ch].end()) {
			ans++;
			lastIndex = *pos[ch].begin();
		}
		else{
			lastIndex = *it;
		}
	}
	cout << ans << ln;
}
int main()
{
#ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
#endif
  fast_cin();
  ll t = 1;
  // cin >> t;
  // for (int it = 1; it <= t; it++)
  // {
  //   //cout << "Case #" << it+1 << ": ";
  //   solve();
  // }
  solve();
  return 0;
}


Comments

Submit
0 Comments
More Questions

1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence