fork(1) download
  1. #ifndef ONLINE_JUDGE
  2. #define gc getchar //getchar_unlocked &putchar_unlocked are faster than others
  3. #define pc putchar
  4. #else
  5. #define gc getchar_unlocked
  6. #define pc putchar_unlocked
  7. #endif
  8.  
  9.  
  10. #include<stdio.h>
  11.  
  12. //#include<iostream>
  13. //#include<>
  14. //using namespace std;
  15.  
  16.  
  17. inline int input( )
  18. {
  19. int n=0;
  20. register int ch=gc();
  21. while( ch >= '0' && ch <= '9' )
  22. {
  23. n = (n<<3)+(n<<1) + ch-'0';
  24. ch=gc();
  25. }
  26. return n;
  27. }
  28.  
  29.  
  30. inline void str_input(char *str)
  31. {
  32. register char c=0;
  33. register int i = 0;
  34.  
  35. c = gc();
  36. while (c!=32 && c!=10) // 32= space and 10= new line. If want to include
  37. { // spaces in string remove c!=32.
  38. str[i] = c;
  39. c = gc();
  40. i = i + 1;
  41. }
  42.  
  43. str[i] = '\0';
  44. }
  45.  
  46.  
  47.  
  48.  
  49. int main()
  50. {
  51. int t;
  52. t=input();
  53. while(t--)
  54. {
  55. int i=1,n=input();
  56. char s[1002];
  57. str_input(s);
  58.  
  59. if(s[0]=='1')
  60. {
  61. s[0]='*';
  62. if(s[1]!='1')
  63. {
  64. s[1]='*';
  65. i=2;
  66. }
  67. }
  68.  
  69. for( ; i<n; i++)
  70. {
  71. if(s[i]=='1')
  72. {
  73. s[i-1]='*';
  74. s[i]='*';
  75. if(s[i+1]!='1' && s[i+1]!='\0')
  76. {
  77. s[i+1]='*';
  78. i++;
  79. }
  80. }
  81. }
  82. int cnt=0;
  83. for( i=0; i<n; i++)
  84. {
  85. if(s[i]!='*')
  86. cnt++;
  87. }
  88. printf("%d \n",cnt);
  89. }
  90. return 0;
  91. }
  92.  
Runtime error #stdin #stdout 0s 2684KB
stdin
1
1
0
stdout
Standard output is empty