fork download
  1. // i wants to take ioi
  2. //binhtinhtutinkhongcaycunhungmotkhikhongcontutinnualatuyetvong
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. #define int long long
  8. #define nn "\n"
  9. #define pi pair<int, int>
  10. #define fi first
  11. #define se second
  12. #define lb lower_bound
  13. #define ub upper_bound
  14. #define eb emplace_back
  15. #define pb push_back
  16. #define TASK " "
  17.  
  18. #define ms(a, x) memset(a, x, sizeof(a))
  19. #define all(a) a.begin(), a.end()
  20. #define All(a, n) a + 1, a + 1 + n
  21.  
  22. #define LOG 19
  23.  
  24.  
  25. const int INF = 1e18;
  26. const int mod = 1e9;
  27. const int N = 1e5 + 5;
  28. const int MOD = 998244353;
  29. int bit[200000];
  30. struct node{
  31. int kc, u, hk;
  32. bool operator<(const node& other) const {
  33. return kc > other.kc;
  34. }
  35. };
  36. struct edge{
  37. int v, w, h;
  38. };
  39. string s;
  40. void nhap(){
  41. cin >> s;
  42. }
  43. void solve(){
  44. map<char, int> cnt;
  45. map<char, int> cur;
  46. int n; cin >> n;
  47. for(int i = 1; i <= n; i++){
  48. char c; int x;
  49. cin >> c >> x;
  50. cnt[c] = x;
  51. }
  52. int ans = 0;
  53. for(int i = 0; i < s.size(); i++){
  54. if(cnt.count(s[i])){
  55. if(cur[s[i]] == 0){
  56. ans++;
  57. cur[s[i]] = cnt[s[i]] - 1;
  58. }
  59. else{
  60. cur[s[i]]--;
  61. }
  62. }
  63. ans++;
  64. }
  65. cout << ans << nn;
  66. }
  67.  
  68. signed main() {
  69. // freopen("piggyback.in", "r", stdin);
  70. // freopen("piggyback.out", "w", stdout);
  71. ios_base::sync_with_stdio(0);
  72. cin.tie(0);
  73. cout.tie(0);
  74. nhap();
  75. int t; cin >> t;
  76. while(t--) solve();
  77. return (0 ^ 0);
  78.  
  79. }
  80.  
Success #stdin #stdout 0.01s 5304KB
stdin
russiaopenhighschoolteamprogrammingcontest
2
s 3
o 5
stdout
42
42