Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream in("stringaib.in");
- ofstream out("stringaib.out");
- struct baza
- {
- int vp,vlg; /// vp retine in ce ordine le citesc, vlg=lungimea sirului
- char *s;
- };
- baza sir[100010];
- int aib[100005],n,ans[100002];
- bool cmp(const baza&a,const baza&b)
- {
- if(a.vlg==b.vlg) return strcmp(a.s,b.s)<0;
- return a.vlg<b.vlg;
- }
- void Update(int i)
- {
- for(;i<=n;i=i+(i&(-i)))
- ++aib[i];
- }
- int Query(int i)
- {
- int s;
- for(s=0;i>0;i=i-(i&(-i)))
- s+=aib[i];
- return s;
- }
- int main()
- {
- in>>n;
- char h[10001];
- for(int i=0;i<n;++i)
- {
- in>>h; /// citesc sirul h
- int lh=strlen(h); /// lh=lungime sir h
- sir[i].vlg=lh;
- sir[i].vp=i+1; /// pentru ca pornesc cu i de la 0, pun i+1
- sir[i].s=(char*)malloc(lh+1); /// aloc spatiu
- strcpy(sir[i].s,h); /// s<-h
- }
- sort(sir,sir+n,cmp); /// sortez sirul
- for(int i=n;i>=1;--i)
- ans[sir[i-1].vp]=Query(sir[i-1].vp),Update(sir[i-1].vp);
- for(int i=1;i<=n;++i)
- out<<ans[i] <<' ';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement