# 牛客网-腾讯编程题

## 1 编码

baca

16331

### 实现

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int main() {
string s;
cin >> s;
if (s.empty() || s.size() > 4) return 0;
int times = 25;
int k = 0, res = 0;
for (int i = 0, p = 4; i < s.size(); i++,p--) {
k = s[i] - 'a';
int pre = 0;
for (int j = 0; j < p; j++) {
pre += pow(times, j);
}
res += k * pre;
if (i != 0) res++;
}
cout << res;
return 0;
}

## 2. 素数对

10

2

### 实现

#include<iostream>
#include<algorithm>

using namespace std;

bool isPrime(int n) {
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}

int main() {
int num;
cin >> num;

// count the pairs
int count = 0;
for (int i = 2; i <= num / 2; i++) {
if (isPrime(i) && isPrime(num - i)) {
count++;
}
}

// print the res
cout << count;
return 0;
}

## 2.

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

// DP
int main() {
string s;
while (cin >> s) { // read data
int n = s.size();
if (n == 0) return 0;

// DP part
vector<vector<int>> dp(n, vector<int>(n, 0));  // dp[i][j] 表示（i, j）之间回文串的最长长度
for (int i = n - 1; i >= 0; i--) {
dp[i][i] = 1;
for (int j = i + 1; j < n; j++) {
if (s[i] == s[j])
dp[i][j] = dp[i + 1][j - 1] + 2;
else
dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]);
}
}

// print the result
cout << n - dp[0][n - 1] << endl;
s.clear();
}
return 0;
}