Advertisement
a53

SecvBiti

a53
Oct 23rd, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. /*
  5. Fie n lungimea șirului s. Se construiește vectorul t[] în care t[0] = 1 și, pentru i=1..n, t[i] = numărul de biți de 1 minus numărul de biți de 0 din primii i biți din șir. Soluția problemei este dată de numărul de perechi (i, j) cu 0 ≤ i < j ≤ n și t[i] = t[j]. Complexitate O(n).
  6. */
  7.  
  8. long long SecvBiti(char s[])
  9. {
  10. int n=0,i=0,nb0=0,nb1=0;
  11. while(s[i]!=0)
  12. ++n;
  13. long long t[10001];
  14. int F[1001];
  15. t[0]=1;
  16. F[0]=1;
  17. for(int i=1;i<=n;++i)
  18. {
  19. if(s[i-1]=='0')
  20. ++nb0;
  21. else
  22. ++nb1;
  23. t[i]=nb1-nb0;
  24. ++F[t[i]];
  25. }
  26. int MAX=F[0];
  27. for(int i=1;i<=n;++i)
  28. cout<<F[i]<<' ';
  29. cout<<endl;
  30. for(int i=1;i<=n;++i)
  31. if(F[i]>MAX)
  32. MAX=F[i];
  33. return MAX;
  34. }
  35.  
  36. int main()
  37. {
  38. char s[100000];
  39. cin>>s;
  40. cout<<SecvBiti(s);
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement