题解 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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现