文章首发于公众号“面鲸“,欢迎关注~I’m waiting for you!
#include
using namespace std;
const int maxn = 30001;
int ans;
vector<int> pre(maxn, 0), mid(maxn, 0);
// 当前树的所有节点,在前序序列中的范围是[l, r],在中序序列中的范围是[ml, mr]
void dfs(int l, int r, int ml, int mr) {
if (l > r) return; // 当前树没节点了
if (l == r) {
// 如果这个树只有一个节点,那它肯定是叶子节点
ans++;
return;
}
int root = pre[l]; // 当前树的跟节点
int index;
for (int i = ml; i <= mr; ++i) {
// 在中序序列中找这个树的左子树范围和右子树范围
if (mid[i] == root) {
index = i;
break;
}
}
int lcnt = index - ml; // 左子树的节点数,利用中序序列中root节点左侧的是左子树,右侧的是右子树
int rcnt = mr - index; // 右子树的节点树
dfs(l + 1, l + lcnt, ml, index - 1); // 递归处理左子树,下同
dfs(l + lcnt + 1, r, index + 1, mr);
}
int main() {
int n;
while(cin >> n) {
for (int i = 1; i <= n; ++i) cin >> pre[i];
for (int i = 1; i <= n; ++i) cin >> mid[i];
ans = 0;
dfs(1, n, 1, n);
cout << ans << endl;
}
return 0;
}
n = int(input())
pre = input().split()
mid = input().split()
ans = 0
def dfs(l, r, ml, mr):
global ans
if l > r: return
if l == r:
ans += 1
return
root = pre[l]
index = ml
for i in range(ml, mr + 1):
if mid[i] == root:
index = i
break
lcnt = index - ml
rcnt = mr - index
dfs(l + 1, l + lcnt, ml, index - 1)
dfs(l + lcnt + 1, r, index + 1, mr)
dfs(0, n - 1, 0, n - 1)
print(ans)
import java.util.Scanner;
public class zijie2_1 {
public static int ans;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int []pre = new int[n + 1];
int []mid = new int[n + 1];
for (int i = 1; i <= n; ++i) {
pre[i] = input.nextInt();
}
for (int i = 1; i <= n; ++i) {
mid[i] = input.nextInt();
}
ans = 0;
dfs(1, n, 1, n, pre, mid);
System.out.println(ans);
}
public static void dfs(int l, int r, int ml, int mr, int[]pre , int[] mid) {
if (l > r) return;
if (l == r) {
ans++;
return;
}
int root = pre[l], index = ml;
for (int i = ml; i <= mr; ++i) {
if (mid[i] == root) {
index = i;
break;
}
}
int lcnt = index - ml, rcnt = mr - index;
dfs(l + 1, l + lcnt, ml, index -1, pre, mid);
dfs(l + lcnt + 1, r, index + 1, mr, pre, mid);
}
}
#include
using namespace std;
int main() {
int n;
string s;
string p = "0010";
while(cin >> s) {
int cnt = 0;
size_t found = s.find(p, 0);
while(found != string::npos) {
++cnt;
found += 3;
if(found >= s.size()) break;
found = s.find(p, found);
}
cout << cnt << endl;
}
return 0;
}
#include
using namespace std;
vector<int> arr(10000, 0);
int main() {
int n, m;
while(cin >> n >> m) {
int l = 0, r = 0;
int ans;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
r = max(r, arr[i]);
}
while (l <= r) {
int mid = (l + r) >> 1; // 检查mid间隔能播放多少广告
int tmp_cnt = 0;
for (int i = 0; i < n; ++i) {
tmp_cnt += (arr[i] / mid + 1);
}
if (tmp_cnt >= m) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans << endl;
}
return 0;
}
#include
using namespace std;
using ll = long long;
vector<ll> a, x, y;
ll m;
void dfs(vector<ll> &vec, int index, int end, ll sum) {
if (index == end) {
return;
}
vec.emplace_back(sum);
dfs(vec, index + 1, end, vec.back());
vec.emplace_back((sum + a[index]) % m);
dfs(vec, index + 1, end, vec.back());
}
int main() {
int n;
ll d, sum = 0;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> d;
d %= m;
if (d != 0) {
a.emplace_back(d);
sum += d;
}
}
if (sum < m) {
cout << sum << endl;
return 0;
}
n = a.size();
dfs(x, 0, n >> 1, 0);
dfs(y, n >> 1, n, 0);
sort(x.begin(), x.end());
sort(y.begin(), y.end());
x.erase(unique(x.begin(), x.end()), x.end());
y.erase(unique(y.begin(), y.end()), y.end());
auto left = x.begin();
auto right = y.rbegin();
ll ans = max(x.back(), y.back()), pre = 0;
while (ans < m - 1 && left != x.end() && right != y.rend()) {
ll temp = (*left + *right) % m;
ans = max(ans, temp);
if (temp > pre) {
++left;
} else {
--right;
}
}
cout << ans << endl;
return 0;
}
面鲸,专注于互联网招聘笔试、面试经验分享,希望能给您带来一丝帮助,欢迎关注~
后台回复联系可添加小编微信加入面试笔试刷题交流群,我们在这里等你~