fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define fi first
  5. #define se second
  6. #define siz(x) (int)(x.size())
  7. #define all(x) x.begin(), x.end()
  8. #define debug_arr(x,len) for(int _=1; _<=len; _++) cout<<x[_]<<" "; cout<<'\n';
  9. #define debug(x) cout<<'\n'<<#x<<": "<<x<<'\n';
  10. const int maxN = 1e6+5;
  11.  
  12. int n, m, k, a[maxN], dp[maxN];
  13. string s;
  14. vector<int>pos[30];
  15.  
  16. void solve()
  17. {
  18.  
  19. }
  20.  
  21. int32_t main()
  22. {
  23. ios_base::sync_with_stdio(0); cin.tie(0);
  24. cin>>n>>k;
  25. cin>>s;
  26. for(int i=0; i<=n; i+=1) dp[i] = 1e18;
  27. dp[n] = 1;
  28. for(int i=n-1; i>=0; i-=1)
  29. {
  30. pos[s[i]-'a'].push_back(i);
  31. bool check = 0;
  32. for(int j=0; j<k; j+=1)
  33. {
  34. if(!pos[j].empty())
  35. {
  36. dp[i] = min(dp[i], dp[pos[j].back()+1] + 1);
  37. }
  38. else check = 1;
  39. }
  40. if(check) dp[i] = 1;
  41. }
  42. // for(int i=0; i<n; i+=1) cout<<dp[i]<<" "; cout<<'\n';
  43. for(int i=0; i<26; i+=1) sort(all(pos[i]));
  44. cin>>m;
  45. while(m--)
  46. {
  47. string s1; cin>>s1;
  48. int cur = 0, ans = -1;
  49. for(int i=0; i<siz(s1); i+=1)
  50. {
  51. auto tmp = lower_bound(all(pos[s1[i]-'a']), cur);
  52. if(*tmp >= cur && *tmp <n)
  53. {
  54. cur = *tmp+1;
  55. }
  56. else ans = 0;
  57. }
  58. if(ans == -1) ans = dp[cur];
  59. cout<<ans<<'\n';
  60. }
  61. }
  62.  
  63. // dp[i] la min so operation de tu vi tri i co the pha duoc subsequence
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty