爱新觉罗·弘历 @ 2018-11-03 13:10:49
using namespace std;
const int L=3001;
int n,q;
string s1[L],s2[L],ans[L];
int len[L];
bool x[L];
void sort();
int main()
{
cin>>n>>q;
for (int i=1;i<=n;i++)
cin>>s1[i];
for (int i=1;i<=q;i++)
cin>>len[i]>>s2[i];
sort();
for (int i=1;i<=q;i++)
for (int j=1;j<=n;j++){
int l=s1[j].length()-1,p=len[i]-1;
bool op=0;
if (p>l||x[j]) continue;
while (p>=0){
// cout<<s2[i][p]<<"-"<<s1[j][l-p-1]<<endl;
if (s2[i][p]!=s1[j][l]){
op=1;
break;
}
p--;l--;
}
if (!op){
ans[i]=s1[j],x[j]=1;
break;
}
}
for (int i=1;i<=q;i++)
if (ans[i].length()!=0) cout<<ans[i]<<endl;
else cout<<-1<<endl;
return 0;
}
void sort()
{
for (int i=n-1;i>=1;i--)
for (int j=1;j<=i;j++){
int l1=s1[j].length(),l2=s1[j+1].length();
if (l1>l2) swap(s1[j],s1[j+1]);
else if (s1[j]>s1[j+1]) swap(s1[j],s1[j+1]);
}
}
by WA鸭鸭 @ 2018-11-03 13:11:07
希望更丰富的展现?使用Markdown
by WA鸭鸭 @ 2018-11-03 13:11:21
希望更丰富的机惨?使用Markdown
by 爱新觉罗·弘历 @ 2018-11-03 13:13:17
#include<algorithm>
#include<iostream>
using namespace std;
const int L=3001;
int n,q;
string s1[L],s2[L],ans[L];
int len[L];
bool x[L];
void sort();
int main()
{
cin>>n>>q;
for (int i=1;i<=n;i++)
cin>>s1[i];
for (int i=1;i<=q;i++)
cin>>len[i]>>s2[i];
sort();
for (int i=1;i<=q;i++)
for (int j=1;j<=n;j++){
int l=s1[j].length()-1,p=len[i]-1;
bool op=0;
if (p>l||x[j]) continue;
while (p>=0){
// cout<<s2[i][p]<<"-"<<s1[j][l-p-1]<<endl;
if (s2[i][p]!=s1[j][l]){
op=1;
break;
}
p--;l--;
}
if (!op){
ans[i]=s1[j],x[j]=1;
break;
}
}
for (int i=1;i<=q;i++)
if (ans[i].length()!=0) cout<<ans[i]<<endl;
else cout<<-1<<endl;
return 0;
}
void sort()
{
for (int i=n-1;i>=1;i--)
for (int j=1;j<=i;j++){
int l1=s1[j].length(),l2=s1[j+1].length();
if (l1>l2) swap(s1[j],s1[j+1]);
else if (s1[j]>s1[j+1]) swap(s1[j],s1[j+1]);
}
}