Advertisement
a53

stringAIB

a53
Feb 2nd, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream in("stringaib.in");
  4. ofstream out("stringaib.out");
  5. struct baza
  6. {
  7. int vp,vlg; /// vp retine in ce ordine le citesc, vlg=lungimea sirului
  8. char *s;
  9. };
  10. baza sir[100010];
  11. int aib[100005],n,ans[100002];
  12.  
  13. bool cmp(const baza&a,const baza&b)
  14. {
  15. if(a.vlg==b.vlg) return strcmp(a.s,b.s)<0;
  16. return a.vlg<b.vlg;
  17. }
  18.  
  19. void Update(int i)
  20. {
  21. for(;i<=n;i=i+(i&(-i)))
  22. ++aib[i];
  23. }
  24.  
  25. int Query(int i)
  26. {
  27. int s;
  28. for(s=0;i>0;i=i-(i&(-i)))
  29. s+=aib[i];
  30. return s;
  31. }
  32.  
  33. int main()
  34. {
  35. in>>n;
  36. char h[10001];
  37. for(int i=0;i<n;++i)
  38. {
  39. in>>h; /// citesc sirul h
  40. int lh=strlen(h); /// lh=lungime sir h
  41. sir[i].vlg=lh;
  42. sir[i].vp=i+1; /// pentru ca pornesc cu i de la 0, pun i+1
  43. sir[i].s=(char*)malloc(lh+1); /// aloc spatiu
  44. strcpy(sir[i].s,h); /// s<-h
  45. }
  46. sort(sir,sir+n,cmp); /// sortez sirul
  47. for(int i=n;i>=1;--i)
  48. ans[sir[i-1].vp]=Query(sir[i-1].vp),Update(sir[i-1].vp);
  49. for(int i=1;i<=n;++i)
  50. out<<ans[i] <<' ';
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement