【模拟】图书管理员

原题传送门

思路


这道题十分简单,半小时切掉,就是代码有点丑QAQ这么菜的题用这么长的代码,不过——哼唧,我敲代码快我任性哈哈。
不讲思路了,太简单了,我用了两个辅助函数,其实应该有相应的库函数可以用,但我懒得查了,况且考场上我也查不了,便自食其力了QAQ

Code


#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<map>
using namespace std;
string p[1005],b[1005];
int x;
int cmp(string per,string book)
{
    int lenp=per.length();
    int lenb=book.length();
    if(lenp>lenb)return 0;
    for(int cosb=lenb-lenp,cosp=0;cosb<lenb;cosb++,cosp++)
    {
        if(book[cosb]!=per[cosp])
        {
            return 0;
        }
    }
    return 1;
}

int turn(string s)
{
    int ans=0,len=s.length();
    for(int i=len-1,w=1;i>=0;i--,w*=10)
    {
        ans+=(s[i]-'0')*w;
    }
    return ans;
}
int main()
{
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
    }
    for(int i=1;i<=q;i++)
    {
        cin>>x>>p[i];
    }
    for(int i=1;i<=q;i++)
    {
        int min=99999999;
        for(int j=1;j<=n;j++)
        {
            if(cmp(p[i],b[j])&&turn(b[j])<min)
            {
                min=turn(b[j]);
            }
        }
        if(min==99999999)
        {
            cout<<-1<<endl;
        }
        else
        {
            cout<<min<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/gongdakai/p/11615142.html