题目在此
忘记打了,补题中
class Solution {
public:
string reversePrefix(string w, char ch) {
string res = "";
int n = w.size();
int id = -1;
for (int i = 0; i < n; i ++){
if (w[i]==ch){
id = i;
break;
}
}
for (int i = id; i >= 0; i --)res += w[i];
for (int i = id + 1; i < n; i ++)res += w[i];
return res;
}
};
约分,排序,求和
typedef long long ll;
class Solution {
public:
ll interchangeableRectangles(vector<vector<int>>& r) {
ll res = 0;
int n = r.size();
for (int i = 0; i < n; i ++){
int z = __gcd(r[i][0],r[i][1]);
r[i][0] = r[i][0]/z;
r[i][1] = r[i][1]/z;
}
sort(r.begin(),r.end(),[](vector<int> &a,vector<int> &b){
if (a[0] == b[0])return a[1] < b[1];
return a[0] < b[0];
});
ll num = 1;
for (int i = 1; i < n; i ++){
if (r[i][0]==r[i-1][0]&&r[i][1]==r[i-1][1])num++;
else {
res += num*(num-1)/2;
num = 1;
}
}
res += num*(num-1)/2;
return res;
}
};
数据较小,直接暴力枚举。
枚举所有子序列,找出其中回文的,逐一比较。
class Solution {
public:
bool jd(string a){ //判回文
for (int i = 0,j = a.size() - 1; i <= j; i ++,j--){
if (a[i]!=a[a.size()-i-1])return false;
}
return true;
}
bool jd1(int a,int b,int n){ //判是否每一位不一样
for (int i = n-1; i >= 0; i --){
int ta = a >> i & 1,tb = b >> i & 1;
if (ta==1&&tb==1)return false;
}
return true;
}
int maxProduct(string s) {
int n = s.size();
int res = 1;
vector<pair<string,int>> vs;
map<string,int> mp;
for (int i = 0; i < (1 << n); i ++){
string a = "";
for (int j = n-1; j >= 0; j --)
if ((i >> j & 1) == 1)a += s[j];
if (!jd(a))continue;
vs.push_back(pair{a,i});
}
int le = vs.size();
for (int i = 0; i < le; i ++){
for (int j = i + 1; j < le; j ++){
if (jd1(vs[i].second,vs[j].second,n)){
int tt = vs[i].first.size()*vs[j].first.size();
res = max(res,tt);
}
}
}
return res;
}
};
待 续 待续 待续