本文最后更新于:2022年8月28日 上午
2022-08-27 美团笔试
第一题
样例:
| input: 7 3 abcaacc a*c
output: 3
|
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
|
#include <bits/stdc++.h> typedef long long LL; using namespace std;
int main() { LL n,m; cin >> n >> m;
string s1, s2;
cin >> s1 >> s2; LL res = 0;
for (int i = 0; i <= n - m; i ++) { bool flag = true;
string tmp = s1.substr(i, m); for (int j = 0; j < m; j++) { if (s2[j] != '*' && tmp[j] != s2[j]) flag = false; }
if (flag) res ++; } cout << res; return 0; }
|
第二题
c++版本
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
|
#include<bits/stdc++.h> using namespace std;
typedef long long LL; typedef pair<int, int> PII;
const int N = 50010;
int main() {
int n, m; cin >> n >> m;
vector<int> a(n, 0);
for (int i = 1; i <= n; i ++) { a[i - 1] = i; }
for (int i = 0; i < m ; i++) { int x; cin >> x; a.erase(find(a.begin(), a.end(), x)); a.insert(a.begin(), x); }
for (auto x : a) printf("%d ", x); return 0; }
|
c++与Java的容器对着
Java版本
其中 c++ 的 map 就是 Java 的 TreeMap, key-value映射,按照key有序
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
| public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); Map<Integer, Integer> k2v = new HashMap<>(); TreeMap<Integer, Integer> v2k = new TreeMap<>((x, y) -> y - x); for (int i = 0; i < m; i++) { int num = sc.nextInt(); if (k2v.containsKey(num)) { int v = k2v.get(num); v2k.remove(v); k2v.put(num, i); v2k.put(i, num); } k2v.put(num, i); v2k.put(i, num); } for (int v : v2k.keySet()) System.out.print(v2k.get(v) + " "); for (int i = 1; i <= n; i++) { if (!k2v.containsKey(i)) System.out.print(i + " "); } } }
|