writer @ 2021-08-30 11:55:58
#include <bits/stdc++.h>
using namespace std;
int n,q,x[1005];
string a[1005],b[1005],c[1005];
int main()
{
cin>>n>>q;
for(int i=0;i<=q-1;i++)
c[i]="11111111";
for(int i=0;i<=n-1;i++)
cin>>a[i];
for(int i=0;i<=q-1;i++)
cin>>x[i]>>b[i];
/*for(int i=0;i<=q-1;i++)
cout<<b[i]<<endl;*/
return 0;
for(int i=0;i<=q-1;i++)
{
for(int i=0;i<=n-1;i++)
{
if(a[i].find(b[i],0)!=string::npos&&a[i].size()<c[i].size())
{
c[i]=a[i];
}
}
}
for(int i=0;i<=q-1;i++)
{
if(c[i]=="11111111")
cout<<-1<<endl;
else
cout<<c[i]<<endl;
}
return 0;
}
by G_Lengyue @ 2021-08-30 15:57:41
你提前return 0干啥
by G_Lengyue @ 2021-08-30 16:13:12
删去了return 0;后,你的样例输出为:
2123
1123
-1
-1
-1
就可以发现是你没有比大小,所以我们再改一下
by G_Lengyue @ 2021-08-30 16:51:28
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n, q, x[1005]; //n为数的数量 q为读者的数量 x[i]为位数
string a[1005], b[1005], c[1005]; // a[i]为图书编码 b[i]为需求码 c[i]为你的答案
int main()
{
cin >> n >> q;
for (int i = 0; i <= q - 1; i ++)
c[i] = "11111111";
for (int i = 0; i <= n - 1; i ++)
cin >> a[i];
for (int i = 0; i <= q - 1; i ++)
cin >> x[i] >> b[i];
//return 0;
for (int i = 0; i <= q - 1; i ++)
{
for (int j = 0; j <= n - 1; j ++)
{
if (a[j].find(b[i], a[j].length() - x[i]) != a[j].npos)
{
if (atoi(c[i].c_str()) > atoi(a[j].c_str()))
{
c[i] = a[j];
}
}
}
}
for (int i = 0; i <= q - 1; i++)
{
if (c[i] == "11111111")
cout << -1 << endl;
else
cout << c[i] << endl;
}
return 0;
}
by G_Lengyue @ 2021-08-30 16:51:55
这是我改进的代码,可以AC,你自己研究一下吧
by writer @ 2021-09-05 10:57:55
@Baojun666 谢谢神犇!感谢!
by writer @ 2021-09-05 11:00:04
@Baojun666 return 0是测试用的,忘删了……