题解 P2412 【查单词】

2021.1.2 Update:更好的排版

你们都用高大上的算法,只有我用暴力。

推广博客:https://www.luogu.com.cn/blog/332914/

本题需要注意的是大小写不敏感,那就全部转为小写呗(大写也可以的)。

对于转换的排个字典序,每次暴力。

最劣复杂度 O(nm),不知道怎么能过。

#include <iostream>
#include <algorithm>
#include <cctype>
#include <cstring>
using namespace std;

struct Node
{
    string s, x;
    int num;
}a[50005];

bool cmp(Node x, Node y)
{
    return x.x > y.x;
}

int main()
{
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for(register int i = 1; i <= n; i++)
    {
        cin >> a[i].s;
        a[i].x = a[i].s;
        transform(a[i].x.begin(), a[i].x.end(), a[i].x.begin(), ::tolower);//转小写
        a[i].num = i;//储存下来
    }
    sort(a + 1, a + n + 1, cmp);
    for(register int i = 1; i <= m; i++)
    {
        int l, r;
        cin >> l >> r;
        for(register int j = 1; j <= n; j++)
        {
            if(a[j].num >= l && a[j].num <= r)
            {
                cout << a[j].s << endl;
                break;//注意退出
            }
        }
    }
    return 0;
}
posted @   HappyBobb  阅读(7)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示