fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int solution(string &S){
  5.  
  6. int n = S.size();
  7. if(n==1 && S[0] == 'H') return -1;
  8. else if(n==1 && S[0] == '-') return 0;
  9. vector<bool> isTank(n,false);
  10. int cnt = 0;
  11. for(int i=0;i<n;i++){
  12. if(S[i] == 'H'){
  13. if(i==0){
  14. if(S[i+1] == 'H') return -1;
  15. isTank[i+1] = true;
  16. cnt++;
  17. }
  18. else{
  19. if(isTank[i-1]) continue;
  20. else if(i+1<n && S[i+1] == '-'){
  21. isTank[i+1] = true;
  22. cnt++;
  23. }
  24. else{
  25. if(S[i-1] == '-'){
  26. isTank[i-1] = true;
  27. cnt++;
  28. }
  29. else{
  30. return -1;
  31. }
  32. }
  33. }
  34. }
  35.  
  36. }
  37.  
  38. return cnt;
  39.  
  40. }
  41.  
  42. int main(){
  43.  
  44.  
  45. string S = "-H-H";
  46. cout<<solution(S);
  47. }
Success #stdin #stdout 0s 5664KB
stdin
Standard input is empty
stdout
1