fork(1) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define f(i,x,y) for(long long i = (x);i < (y);++i)
  4. #define MOD 1000000007
  5.  
  6.  
  7. int main()
  8. {
  9. long long T;
  10. cin >> T;
  11. while(T--)
  12. {
  13. string txt,pat;
  14. cin >> txt >> pat;
  15. long long n = txt.size(),m = pat.size();
  16. long long arr[m];
  17. map<char,vector<long long> > mp;
  18. map<char,vector<long long> > ::iterator it;
  19. f(i,0,m)
  20. {
  21. mp[pat[i]].push_back(i);
  22. arr[i] = 0;
  23. }
  24.  
  25. for(long long i = (n-1);i > -1;i--)
  26. {
  27. char ch = txt[i];
  28. if(mp.find(ch) != mp.end())
  29. {
  30. f(k,0,mp[ch].size())
  31. {
  32. long long j = mp[ch][k];
  33. if(j == (m-1))
  34. arr[j]++;
  35. else if(j < (m-1))
  36. arr[j] += arr[j+1];
  37. else
  38. {;}
  39. }
  40. }
  41. }
  42. //f(i,0,m)
  43. //cout << arr[i] << "-";
  44. cout <<arr[0] << endl;
  45. }
  46. }
  47.  
Success #stdin #stdout 0s 3476KB
stdin
2
00000000001111111111222222222233333333334444444444
0000011111222223333344444
babaabab
bab
stdout
1016255020032
14