捕获.PNG捕获2.PNG

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>
#include <string>
using namespace std;

const int N = 1000+50;

vector<string> bookCode;
vector<string> userCode;


//return true s1比s2大
int check(string s1,string s2){
if(s1.size()==s2.size()&&s1.find(s2)!=string::npos) return -1;
if(s1.size()==s2.size()) {
int m = s1.size();
for(int i=0;i<m;i++){
if((s1[i]-'0')>(s2[i]-'0')) return 1;
else if((s1[i]-'0')<(s2[i]-'0')) return 0;
}
}
return s1.size()>s2.size()?1:0;
}


void quick_sort(vector<string> &bookCode,int l,int r){
if(l>=r) return;
string x = bookCode[(l+r)>>1];
int xl = x.size(),i=l-1,j=r+1;
while(i<j){
do i++;while(check(bookCode[i],x)==0);
do j--;while(check(bookCode[j],x)==1);
if(i<j) {
string tmp = bookCode[i];
bookCode[i] = bookCode[j];
bookCode[j] = tmp;
}
}
quick_sort(bookCode,l,j);
quick_sort(bookCode,j+1,r);
}


string getUserCode(string userCode){
for(int i=0;i<bookCode.size();i++){
string sub = bookCode[i].size()>userCode.size()?bookCode[i].substr(bookCode[i].size()-userCode.size()):bookCode[i];
if(sub.find(userCode)!=string::npos) return bookCode[i];
}
return "-1";
}


int main()
{
int n,q;
cin>>n>>q;
string s;
while(n--) {
cin>>s;
bookCode.push_back(s);
}
quick_sort(bookCode,0,bookCode.size()-1);
for(int i=0;i<q;i++){
int x;
string y;
cin>>x>>y;
userCode.push_back(y);
}
for(int i=0;i<userCode.size();i++) cout << getUserCode(userCode[i]) << endl;
return 0;
}

(其实数据范围就1e7,用不上快排+字符串高精度,直接暴力也能ac。纯当熟悉下c++的string类了)
以上代码就是单纯的模拟,记录一下string下的常用库函数

1
#include <string> //头文件
1
2
3
4
5
//常用函数
str.size():int //返回str的长度
str.find(string v):string //返回v在str中出现的第一个位置,未出现过则返回 string::npos (这是一个非常大的数)
str.substr(int start,int end):string //截取子串
str.substr(int idx):string //同上,从idx截取到尾