fork download
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4. #include<stdlib.h>
  5. using namespace std;
  6. int main()
  7. {
  8.  
  9. int nooftestcases;
  10. int head=0;
  11. int tail=0;
  12.  
  13. cin>>nooftestcases;
  14.  
  15. for(int i=0;i<nooftestcases;i++)
  16. {
  17. int total=0;
  18. int lengthofstring;
  19.  
  20. cin>>lengthofstring;
  21. string str;
  22.  
  23. cin>>str;
  24. for(int j=0;j<lengthofstring;j++)
  25. {
  26. if(str[j]=='T' && head==0)
  27. {
  28. total=-1;
  29. break;
  30.  
  31. }
  32. else if(str[j]=='T' && head==1)
  33. {
  34. total=total+1;
  35. // cout<<"found tail"<<endl;
  36. head=0;
  37. }
  38. else if(str[j]=='H'&& tail==0 && head==0)
  39. {
  40. head=1;
  41. // cout<<"found head"<<endl;
  42. total =total+1;
  43. }
  44. else if(str[j]=='H'&& tail==0 && head==1)
  45. {
  46. total=-1;
  47. break;
  48. }
  49.  
  50. }
  51. // cout<<total<<endl;
  52. if(total%2==0||total==0)
  53. {
  54. cout<<"Valid"<<endl;
  55. }
  56. else
  57. {
  58. cout<<"Invalid"<<endl;
  59. }
  60. }
  61.  
  62. }
Success #stdin #stdout 0s 15240KB
stdin
7
5
1 2 3 2 1
7
2 3 4 5 4 3 2
5
1 2 3 4 3
5
1 3 5 3 1
7
1 2 3 4 3 2 1
4
1 2 3 2
4
1 2 2 1
stdout
Valid
Valid
Valid
Valid
Valid
Valid
Valid