时间复杂度 O ( n ) 时间复杂度O(n) 时间复杂度O(n)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
map<char , char> mp;
string s;
signed main(){
mp['A'] = 'A';
mp['b'] = 'd';
mp['d'] = 'b';
mp['H'] = 'H';
mp['I'] = 'I';
mp['M'] = 'M';
mp['O'] = 'O';
mp['o'] = 'o';
mp['p'] = 'q';
mp['q'] = 'p';
mp['T'] = 'T';
mp['U'] = 'U';
mp['V'] = 'V';
mp['v'] = 'v';
mp['w'] = 'w';
mp['W'] = 'W';
mp['X'] = 'X';
mp['x'] = 'x';
mp['Y'] = 'Y';
cin >> s;
int n = s.size();
s = '?' + s;
bool tag = 1;
if(n & 1) {
int mid = (n + 1) / 2;
if(mp[s[mid]] != s[mid]) tag = 0;
for(int i = 1 ; i <= n / 2 ; i ++) {
if(mp[s[i]] != s[n - i + 1]) tag = 0;
}
} else {
for(int i = 1 ; i <= n / 2 ; i ++) {
if(mp[s[i]] != s[n - i + 1]) tag = 0;
}
}
if(tag) cout << "TAK";
else cout << "NIE";
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
d p [ i ] 表示以 1 − i 子集的最小花费 dp[i] 表示以 ~1-i~ 子集的最小花费 dp[i]表示以 1−i 子集的最小花费
d p [ i ] = m i n ( d p [ i ] , d p [ i − 1 ] + v a l [ i ] ) dp[i] = min(dp[i] ,dp[i-1]+val[i]) dp[i]=min(dp[i],dp[i−1]+val[i])
时间复杂度 O ( n l o g n ) 时间复杂度O(nlogn) 时间复杂度O(nlogn)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int n;
int v[N];
string s;
vector<tuple<string , int , int>> ve;
signed main(){
IOS
cin >> n;
for(int i = 1 ; i <= n ; i ++) {
cin >> v[i];
}
for(int i = 1 ; i <= n ; i ++) {
cin >> s;
ve.emplace_back(s , i , 0);
reverse(s.begin() , s.end());
ve.emplace_back(s , i , v[i]);
}
sort(ve.begin() , ve.end());
vector<int>dp(n + 1 , INF);
dp[0] = 0;
for(int i = 0 ; i < n + n ; i ++) {
int now = get<1>(ve[i]);
int val = get<2>(ve[i]);
dp[now] = min(dp[now] , dp[now - 1] + val);
}
cout << (dp[n] == INF ? -1 : dp[n]) ;
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
时间复杂度: O ( ∑ i = 1 m l e n i ) 时间复杂度:O(\sum_{i=1}^{m}len_i) 时间复杂度:O(i=1∑mleni)
时间复杂度 O ( 36 m ) 时间复杂度O(36m) 时间复杂度O(36m)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int n , q , l , r;
string s , ss[7];
string st = "abc";
int cnt[N][3][3];
/*
前缀 i 三种位置 , 三个字符的个数
*/
signed main(){
IOS
cin >> n >> q;
cin >> s;
s = '?' + s;
for(int i = 1 ; i <= n ; i ++) {
cnt[i][0][0] = cnt[i - 1][0][0] + 1 * (s[i] == 'a') * (i % 3 == 1);
cnt[i][0][1] = cnt[i - 1][0][1] + 1 * (s[i] == 'b') * (i % 3 == 1);
cnt[i][0][2] = cnt[i - 1][0][2] + 1 * (s[i] == 'c') * (i % 3 == 1);
cnt[i][1][0] = cnt[i - 1][1][0] + 1 * (s[i] == 'a') * (i % 3 == 2);
cnt[i][1][1] = cnt[i - 1][1][1] + 1 * (s[i] == 'b') * (i % 3 == 2);
cnt[i][1][2] = cnt[i - 1][1][2] + 1 * (s[i] == 'c') * (i % 3 == 2);
cnt[i][2][0] = cnt[i - 1][2][0] + 1 * (s[i] == 'a') * (i % 3 == 0);
cnt[i][2][1] = cnt[i - 1][2][1] + 1 * (s[i] == 'b') * (i % 3 == 0);
cnt[i][2][2] = cnt[i - 1][2][2] + 1 * (s[i] == 'c') * (i % 3 == 0);
}
for(int i = 1 ; i <= 6 ; i ++) {
ss[i] = st;
next_permutation(st.begin() , st.end());
}
for(int i = 1 ; i <= q ; i ++) {
cin >> l >> r;
int minn = INF;
for(int j = 1 ; j <= 6 ; j ++) {
int fi = ss[j][0] - 'a';
int se = ss[j][1] - 'a';
int th = ss[j][2] - 'a';
int waste = 0;
waste += cnt[r][0][se] - cnt[l - 1][0][se];
waste += cnt[r][0][th] - cnt[l - 1][0][th];
waste += cnt[r][1][fi] - cnt[l - 1][1][fi];
waste += cnt[r][1][th] - cnt[l - 1][1][th];
waste += cnt[r][2][se] - cnt[l - 1][2][se];
waste += cnt[r][2][fi] - cnt[l - 1][2][fi];
minn = min(minn , waste);
}
cout << minn << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
/*
5 1
baacb
2 3
*/