Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
1,324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. //
  2. // Created by Naman Bhalla on 18/10/19.
  3. //
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7.  
  8. int pos[(1 << 20)];
  9. int ans[1000006];
  10.  
  11. int main(){
  12. ios_base::sync_with_stdio(0);
  13. cin.tie(NULL);
  14. cout.tie(NULL);
  15.  
  16. int t;
  17. cin >> t;
  18.  
  19. while(t--){
  20. memset(pos, -1, sizeof pos);
  21.  
  22. int n, k;
  23. cin >> n >> k;
  24.  
  25. string s;
  26. cin >> s;
  27.  
  28. // vector< string > vs{};
  29. // vector< int > bm{};
  30. string now;
  31.  
  32. for(int i = 0; i < k; ++i){
  33. cin >> now;
  34.  
  35. // vs.push_back(now);
  36. int b = 0;
  37.  
  38. for(auto &c: now){
  39. b |= (1 << (c - 'a'));
  40. }
  41.  
  42. // bm.push_back(b);
  43. if(pos[b] == -1){
  44. pos[b] = i + 1;
  45. }
  46. }
  47.  
  48. for(int i = (1 << 20) - 1; i >= 0; --i){
  49. if(pos[i] == -1){
  50. for(int j = 0; j < 20; ++j){
  51. if((1 & (i >> j)) == 0 and pos[i | (1 << j)] != -1){
  52. pos[i] = pos[i | (1 << j)];
  53. break;
  54. }
  55. }
  56. }
  57. }
  58.  
  59.  
  60. int l = 0;
  61. int cnum = 0;
  62. int cval = -1;
  63. int nextnum;
  64.  
  65. for(int i = 0; i < n; ++i){
  66. nextnum = cnum | (1 << (s[i] - 'a'));
  67. if(pos[nextnum] == -1){
  68. for(int j = l; j < i; ++j){
  69. ans[j] = cval;
  70. }
  71. cnum = (1 << (s[i] - 'a'));
  72. cval = pos[(1 << (s[i] - 'a'))];
  73. nextnum = 0;
  74. l = i;
  75. } else {
  76. cval = pos[nextnum];
  77. cnum = nextnum;
  78. }
  79. }
  80.  
  81. if(l < n){
  82. for(int j = l; j < n; ++j){
  83. ans[j] = cval;
  84. }
  85. }
  86.  
  87. for(int i = 0; i < n; ++i){
  88. cout << ans[i] << " ";
  89. }
  90. cout << "\n";
  91.  
  92. }
  93.  
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement