fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cctype>
  4. using namespace std;
  5.  
  6. bool check (int n, string s);
  7.  
  8. int main (){
  9. int t,n;
  10. string s;
  11. cin >> t;
  12. while (t--) {
  13. cin >> n;
  14. cin >> s;
  15.  
  16. cout << (check(n,s) ? "yes" : "no" ) <<endl;
  17.  
  18. }
  19. return 0;
  20. }
  21.  
  22.  
  23. bool check (int n, string m) {
  24. string s;
  25. s=m;
  26. if (n == 1) {
  27. return true;
  28. }
  29. replace(s.begin(),s.end(),s[0],'0');
  30. for (int i=1; i<n; i++){
  31. if (isalpha(s[i])) {
  32.  
  33. if(i==0 || s[i-1]=='0') {
  34. replace(s.begin(),s.end(),s[i],'1');
  35. }
  36. else if (s[i-1]=='1') {
  37. replace(s.begin(),s.end(),s[i],'0');
  38. }
  39.  
  40. }
  41.  
  42.  
  43. if (s[i] == s[i-1]) {
  44. return false;
  45. }
  46.  
  47. }
  48. return true;
  49. }
Success #stdin #stdout 0s 5320KB
stdin
1
2
aa
stdout
yes