5197

4小时前

class Solution {
public:
string reverseWords(string s) {
if(s.empty()) return s;

reverse(s.begin(), s.end());
int idx = 0, j = 0;//j表示的是这个单词的开始下标， idx表示当前单词的结束下标

while(idx < s.size()){
if(s[idx] == ' '){
reverse(s.begin() + j, s.begin() + idx);
j = idx + 1;
}
idx++;
}
reverse(s.begin() + j, s.begin() + s.size());
return s;
}
};


14天前
class Solution {
public:
int findMin(vector<int>& numbers) {
int n = numbers.size();
if(!n) return -1;
int i = 0;
for(i = 0; i < n; i++){
if(numbers[i] < numbers[n - 1]) break;
}
return numbers[i];
}
};


2个月前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 10005, M = N * 2;
int d1[M], d2[M];//每个结点往下走的最大值和次大值,下标存储的是结点的编号
int h[M], e[M], w[M], ne[M], idx;
int p1[M];//p1存储的是该结点的相邻结点
int up[M];//往上走的最远距离
int n;//n个点
const int INF = 0x3F3F3F3F;
int dfs_d(int u, int father)
{
d1[u] = d2[u] = -INF;//求取max,赋值-INF
for(int i = h[u]; i != -1; i = ne[i])//遍历所有相邻结点
{
int j = e[i];//j是相邻点的编号
if(j == father) continue;//不走回父亲
int d = dfs_d(j, u) + w[i];//用子节点来更新父节点
if(d >= d1[u])
{
d2[u] = d1[u];
d1[u] = d;
p1[u] = j;//p1用来在dfs_u中判断,下标存储的是它的最大路径走过的第一个结点
}
else if(d >= d2[u])
{
d2[u] = d;
}
}
if(d1[u] == -INF)//d1[u]是叶子结点,没有更新,赋值为0
d1[u] = d2[u] = 0;
return d1[u];
}

void dfs_u(int u, int father)
{
for(int i = h[u]; i != -1; i = ne[i]){
int j = e[i];
if(j == father) continue;//不走回父亲
if(p1[u] == j)
up[j] = max(up[u], d2[u]) + w[i];//经过当前点选择次大的路径值
else up[j] = max(up[u], d1[u]) + w[i];//没经过当前点往下走
dfs_u(j, u);
}
}

void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

int main()
{
cin >> n;
memset(h, -1, sizeof h);
int a, b, c;
for(int i = 1; i <= n - 1; ++i){
cin >> a >> b >> c;
}

dfs_d(1, -1);//往下深搜
dfs_u(1, -1);//往下深搜,但这次遍历当前结点的子结点的时候更新这些子结点的up值,将u当作这些结点往上走的第一个结点

int res = INF;
for(int i = 1; i <= n; ++i)
res = min(res, max(d1[i], up[i]));

cout << res;

return 0;
}


2个月前
#include<iostream>
#include<cstring>

using namespace std;
const int N = 10010;

int n;
int h[N], idx, ne[N * 2], e[N * 2], w[N * 2];   //边有来回两条
int ans;

void add(int a, int b, int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}

int dfs(int u, int father){ //u表示的是当前浏览到的点，father表示当前点的父节点
int dist = 0, d1 = 0, d2 = 0;   //d1表示的是距离最大的点

for(int i = h[u]; ~i; i = ne[i]){
int j = e[i];
if(j == father) continue;

int d = dfs(j, u) + w[i];
if(d >= d1) d2  = d1, d1 = d;  //更新最大值和第二大值
else if(d > d2) d2 = d;     //只更新第二大值
dist = max(dist, d);
}
ans = max(ans, d1 + d2);
return dist;
}

int main(){
cin >> n;

memset(h, -1, sizeof h);    //初始化一定不能忘了
for(int i = 0; i < n - 1; i ++){
int a, b, c;
cin >> a >> b >> c;
}

dfs(1, -1);     //-1表示的是根节点的父节点

cout << ans << endl;
return 0;
}


2个月前
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

const int N = 15, M = 9;
const double INF = 1e9;

int n, m = 8;
int s[M][M];
double f[M][M][M][M][N];
double X;

int get_sum(int x1, int y1, int x2, int y2)
{
return s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1];
}

double get(int x1, int y1, int x2, int y2)
{
double sum = get_sum(x1, y1, x2, y2) - X;
return (double)sum * sum / n;
}

double dp(int x1, int y1, int x2, int y2, int k)
{
double &v = f[x1][y1][x2][y2][k];
if (v >= 0) return v;
if (k == 1) return v = get(x1, y1, x2, y2);

v = INF;
for (int i = x1; i < x2; i ++ )
{
v = min(v, get(x1, y1, i, y2) + dp(i + 1, y1, x2, y2, k - 1));
v = min(v, get(i + 1, y1, x2, y2) + dp(x1, y1, i, y2, k - 1));
}

for (int i = y1; i < y2; i ++ )
{
v = min(v, get(x1, y1, x2, i) + dp(x1, i + 1, x2, y2, k - 1));
v = min(v, get(x1, i + 1, x2, y2) + dp(x1, y1, x2, i, k - 1));
}

return v;
}

int main()
{
cin >> n;
for (int i = 1; i <= m; i ++ )
for (int j = 1; j <= m; j ++ )
{
cin >> s[i][j];
s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
}

X = (double)s[m][m] / n;
memset(f, -1, sizeof f);
printf("%.3lf\n", sqrt(dp(1, 1, 8, 8, n)));

return 0;
}



2个月前
#include<iostream>

using namespace std;
const int N = 35;

int f[N][N], g[N][N];   //f表示的是区间[i, j]里面， 即中序遍历[i, j]上二叉树的集合, g表示的是区间[i, j]上根节点的编号
int n, w[N];        //记录下根节点以便以后进行前序遍历

void dfs(int l, int r)
{     //对左右区间进行枚举
if(l > r) return ;
int root = g[l][r];     //先找出根节点；
cout << root << ' ' ;
dfs(l, root - 1);
dfs(root + 1, r);

}

int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> w[i];
}

for(int len = 1; len <= n; len++){
for(int i = 1; i + len - 1 <= n; i++){
int j = i + len - 1;
if(len == 1){
f[i][j] = w[i]; //如果长度为1， 这个区间内的加分就是该节点的值
g[i][j] = i;
}
else {

for(int k = i; k <= j; k ++){
int left = k == i? 1 : f[i][k - 1];
int right = k == j? 1 : f[k + 1][j];
int score = left * right + w[k];   //先记录下以k为分界点的子树的加分
if(f[i][j] < score){
g[i][j] = k;
f[i][j] = score;
}

}

}

}
}

cout << f[1][n] << endl;
dfs(1, n);

return 0;
}


2个月前
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 55, M = 35, INF = 1e9;

int n;
int w[N];
LL f[N][N][M];

{
static LL c[M];
memset(c, 0, sizeof c);
for (int i = 0, t = 0; i < M; i ++ )
{
t += a[i] + b[i];
c[i] = t % 10;
t /= 10;
}
memcpy(a, c, sizeof c);
}

void mul(LL a[], LL b)
{
static LL c[M];
memset(c, 0, sizeof c);
LL t = 0;
for (int i = 0; i < M; i ++ )
{
t += a[i] * b;
c[i] = t % 10;
t /= 10;
}
memcpy(a, c, sizeof c);
}

int cmp(LL a[], LL b[])
{
for (int i = M - 1; i >= 0; i -- )
if (a[i] > b[i]) return 1;
else if (a[i] < b[i]) return -1;
return 0;
}

void print(LL a[])
{
int k = M - 1;
while (k && !a[k]) k -- ;   //去除前导0
while (k >= 0) cout << a[k -- ];
cout << endl;
}

int main()
{
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> w[i];

LL temp[M];
for (int len = 3; len <= n; len ++ )
for (int l = 1; l + len - 1 <= n; l ++ )
{
int r = l + len - 1;
f[l][r][M - 1] = 1;
for (int k = l + 1; k < r; k ++ )
{
memset(temp, 0, sizeof temp);
temp[0] = w[l];
mul(temp, w[k]);
mul(temp, w[r]);
if (cmp(f[l][r], temp) > 0)
memcpy(f[l][r], temp, sizeof temp);
}
}

print(f[1][n]);

return 0;
}


2个月前
#include<iostream>
#include<cstring>

using namespace std;

const int N = 210;
int n, m;
int f[N][N];    //f[i][j]表示的是区间[i, j]上的矩阵合并在一起的集合
int w[N];

int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> w[i];
w[i + n] = w[i];
}

for(int len = 2; len <= n + 1; len ++){ //如果长度小于3， 只存在一个矩阵， 无法产生能量，不用枚举
for(int i = 1; i + len - 1 <= n * 2; i ++){
int j = i + len - 1;
for(int k = i + 1; k < j; k ++){
f[i][j] = max(f[i][j], f[i][k] + f[k][j] + w[i] * w[j] * w[k]);
}
}
}

int res = 0;
for(int i = 1; i <= n; i ++){
res = max(res, f[i][i + n]);
}
cout << res << endl;
return 0;
}


2个月前
//缺口存在的地方可能有n种，就是以1 ~ n为断点, 为了避免枚举n次，用2*n长度的线代替

#include<iostream>
#include<cstring>
using namespace std;

const int N = 410;

int f[N][N], g[N][N];   //f保存的是最大值  g保存的是最小值
int n;
int w[N];
int s[N];       //处理前缀和，可以批量对一定区间内的数进行加减

int main(){
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> w[i];
w[i + n] = w[i];
}
for(int i = 1; i <= N; i++) s[i] = s[i - 1] + w[i];

memset(f, -0x3f, sizeof f);
memset(g, 0x3f, sizeof g);

for(int len = 1; len <= n; len ++)
{
for(int i = 1; i + len - 1 <= 2 * n; i++)    //i表示的是左端点， j表示的是右端点
{
int j = i + len - 1;
if(i == j) f[i][j] = g[i][j] = 0;
else{
for(int k = i; k <= j; k ++){
f[i][j] = max(f[i][k] + f[k + 1][j] + s[j] - s[i - 1], f[i][j]);
g[i][j] = min(g[i][k] + g[k + 1][j] + s[j] - s[i - 1], g[i][j]);
}
}

}
}

int maxv = -1e9, minv = 1e9;
for(int i = 1; i <= n; i++){
maxv= max(maxv, f[i][i + n - 1]);
minv = min(minv, g[i][i + n - 1]);
}

cout << minv << endl << maxv << endl;
return 0;
}


2个月前
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 52;
int n, m;
int f[N * 2][N][N], w[N][N];

int main(){
cin >> n >> m;

for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> w[i][j];

for(int i = 2; i <= m + n; i ++)
{
for(int x1 = max(1, i - m); x1 <= min(n, i - 1); x1 ++){
for(int x2 = max(1, i - m); x2 <= min(n, i - 1); x2++){
int t = w[x1][i - x1];
if(x1 != x2) t += w[x2][i - x2];

for(int a = 0; a <= 1; a++)
for(int b = 0; b <= 1; b++){
f[i][x1][x2] = max(f[i][x1][x2], f[i - 1][x1 - a][x2 - b] + t);
}
}
}
}

cout << f[n + m][n][n] << endl;
return 0;
}