https://www.acwing.com/problem/content/1998/
我们开四个string数组,然后前两个分别存储的是升序字符串序列和降序字符串序列,然后第三四个同理,然后对前两个进行sort排序,排完序后我们根据之前的c、d数组对a、b数组进行二分搜索,当然可以直接使用lowerbound或者upperbound,当然也可以自己手写,我这里直接用的STL,详情请看代码
#include<bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000009 #define endl "\n" #define PII pair<int,int> ll ksm(ll a,ll b) { ll ans = 1; for(;b;b>>=1LL) { if(b & 1) ans = ans * a % mod; a = a * a % mod; } return ans; } ll lowbit(ll x){return -x & x;} const int N = 1e5+10; string a[N],b[N],c[N],d[N]; int main() { int n; cin>>n; for(int i = 0;i < n; ++i) { cin>>a[i]; sort(a[i].begin(),a[i].end());//a存储的是升序字典序的字符串序列 c[i] = a[i]; b[i]=a[i]; reverse(b[i].begin(),b[i].end());//b存储的是降序字典序的字符串序列 d[i] = b[i]; } sort(a,a+n); sort(b,b+n); for(int i = 0;i < n; ++i) { int l = lower_bound(b,b+n,c[i]) - b + 1;//因为我们想寻找的是大于等于的位置,所以需要加一 int r = upper_bound(a,a+n,d[i]) - a;//我们寻找的位置是大于d的第一个位置,所以不需要我们认为的加一 cout<<l<<" "<<r<<endl; } return 0; }