fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. /* how many LC Subsequence are there in a string .We will count the number of L ending at index i
  6. if s[i]=="C" , this will give us total number of LC subsequence.
  7. */
  8.  
  9. string s="LCCTLLCT";
  10. int n=s.size();
  11. vector<int> pre(n+1,0);
  12. vector<int> suf(n+1,0);
  13. int count1=0 , count2=0;
  14. for(int i=0;i<n;i++){
  15. if(s[i]=='L'){
  16. count++;
  17. }
  18. pre[i]=count;
  19. }
  20. for(int i=n;i>=0;i--){
  21. if(s[i]=='T'){
  22. count++;
  23. }
  24. suf[i]=count;
  25. }
  26.  
  27.  
  28. int count=0;
  29. unordered_map<string,int> ok;
  30. for(int i=0;i<s.size();i++){ /* ye to humne sidhe order main insert karwaya*/
  31. if(s[i]=='C'){
  32. ok["LC"]+=ok["L"];
  33. }else if(s[i]=='L'){
  34. ok["L"]++;
  35. }else if(s[i]=='T'){
  36. ok["LCT"]+=ok["LC"];
  37. }
  38. }
  39. int anst=ok["LCT"]+ok["LC"];
  40. cout<<ok["LCT"];
  41.  
  42.  
  43.  
  44.  
  45. // what if mujhe piche se(backwards) se padhna ho to
  46. unordered_map<string,int> ok;
  47. for(int i=n-1;i>=0;i--){
  48. if(s[i]=='T'){
  49. ok["T"]++;
  50. }else if(s[i]=='C'){
  51. ok["CT"]+=ok["T"];
  52. }else if(s[i]=='L'){
  53. ok["LCT"]+=ok["CT"];
  54. }
  55. }
  56. int ansl=ok["LCT"]+ok["CT"];
  57. cout<<ok["LCT"];
  58.  
  59.  
  60.  
  61.  
  62. return 0;
  63. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
7