先将每个字符串按照字典序升序排序,存入strmin。
再将每个字符串按照字典序降序排序,存入strmax。
然后对于字符串i,其lowest的值为该字符串的字典序升序在strmax中的位置。
其hightest的值为该字符串的字典序降序在strmin中的位置再减去1,因为在strmin中的该位置前还有一个是该字符串的字典序升序,不应该考虑进去。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct st
{
char str[25];
}strmin[50005],strmax[50005];
char str[50005][25];
char now[25];
bool cmp(char a,char b)
{
return a>b;
}
bool cmp2(struct st a,struct st b)
{
if (strcmp(a.str,b.str)<0)
return true;
return false;
}
int binary_search_low(int l,int r)
{
int mid=(l+r)/2;
if (l<=r)
{
if (strcmp(now,strmax[mid].str)<=0)
return binary_search_low(l,mid-1);
else
return binary_search_low(mid+1,r);
}
return l;
}
int binary_search_high(int l,int r)
{
int mid=(l+r)/2;
if (l<=r)
{
if (strcmp(now,strmin[mid].str)<0)
return binary_search_high(l,mid-1);
else
return binary_search_high(mid+1,r);
}
return l-1;
}
int main()
{
int i,j;
int n;
scanf("%d",&n);
for (i=0;i<n;i++)
{
char s[25];
scanf("%s",s);
int length=strlen(s);
sort(s,s+length);
strcpy(strmin[i].str,s);
sort(s,s+length,cmp);
strcpy(strmax[i].str,s);
strcpy(str[i],s);
}
sort(strmin,strmin+n,cmp2);
sort(strmax,strmax+n,cmp2);
for (i=0;i<n;i++)
{
strcpy(now,str[i]);
int length=strlen(now);
sort(now,now+length);
int low=binary_search_low(0,n-1)+1;
sort(now,now+length,cmp);
int high=binary_search_high(0,n-1)+1;
printf("%d %d\n",low,high);
}
}