【NOIP2017】【Luogu3955】图书管理员(枚举,取模得后缀)

problem

  • n个图书编号,q个需求码。(都是数字)
  • 求满足所有包含需求码后缀的图书编号的最小值。
  • n,q<1e3, 编号需求<1e7

solution

看完题第一个想法字典树模板,,然而不会写?
再看,普及组的题?,,,范围1e3,,平方都能过吧,,怕不是个枚举。。。
于是
——

  • 把图书编号从小到大排序
  • 对于每个需求码,从小到大遍历图书编号,判断是否为后缀(取出最后相应位数进行比较),若是则输出。(括号里坑点,不容易想到,,,%10^n取最后n位这个小常识。
    复杂度O(qn)

codes

#include<iostream>
#include<algorithm>
using namespace std;
const int pow[] = {1,10,100,1000,10000,100000,1000000,10000000};//7位
const int maxn = 1010;
int a[maxn];
int main(){
    int n, q;
    cin>>n>>q;
    for(int i = 1; i <= n; i++)cin>>a[i];
    sort(a+1,a+n+1);
    while(q--){
        int x, y, i;
        cin>>x>>y;
        for(i = 1; i <= n; i++)
            if(a[i]%pow[x] == y){cout<<a[i]<<'\n';break;}
        if(i == n+1)cout<<"-1"<<'\n';
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33957603/article/details/81175299
今日推荐