题目
解题思路
- 使用哈希表(字典)统计每个字符出现的次数
- 找出最小出现次数
- 遍历原字符串,只保留出现次数大于最小次数的字符
代码
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main() {
string str;
cin >> str;
// 统计字符频率
unordered_map<char, int> freq;
for (char c : str) {
freq[c]++;
}
// 找出最小频率
int minFreq = str.length();
for (auto& pair : freq) {
minFreq = min(minFreq, pair.second);
}
// 构建结果字符串
string result;
for (char c : str) {
if (freq[c] > minFreq) {
result += c;
}
}
cout << result << endl;
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
// 统计字符频率
Map<Character, Integer> freq = new HashMap<>();
for (char c : str.toCharArray()) {
freq.put(c, freq.getOrDefault(c, 0) + 1);
}
// 找出最小频率
int minFreq = str.length();
for (int count : freq.values()) {
minFreq = Math.min(minFreq, count);
}
// 构建结果字符串
StringBuilder result = new StringBuilder();
for (char c : str.toCharArray()) {
if (freq.get(c) > minFreq) {
result.append(c);
}
}
System.out.println(result.toString());
}
}
}
s = input()
# 统计字符频率
freq = {}
for c in s:
freq[c] = freq.get(c, 0) + 1
# 找出最小频率
min_freq = min(freq.values())
# 构建结果字符串
result = ''.join(c for c in s if freq[c] > min_freq)
print(result)
算法及复杂度
- 算法:哈希表统计 + 字符串处理
- 时间复杂度: O ( n ) \mathcal{O}(n) O(n),其中 n 为字符串长度
- 空间复杂度: O ( k ) \mathcal{O}(k) O(k),其中 k 为不同字符的个数,最大为26(因为题目限定只有小写字母)
解题思路
-
题目要求:
- 计算两个字符串的最长公共子串长度
- 子串必须连续
- 只包含小写字母
- 字符串长度: 1 ≤ s ≤ 150 1 \leq s \leq 150 1≤s≤150
-
解题方法:动态规划
- 使用二维dp数组记录公共子串长度
- d p [ i ] [ j ] dp[i][j] dp[i][j] 表示以 s t r 1 [ i ] str1[i] str1[i] 和 s t r 2 [ j ] str2[j] str2[j] 结尾的最长公共子串长度
- 如果当前字符相同,则 d p [ i ] [ j ] = d p [ i − 1 ] [ j − 1 ] + 1 dp[i][j] = dp[i-1][j-1] + 1 dp[i][j]=dp[i−1][j−1]+1
代码
def longest_common_substring(str1, str2):
m, n = len(str1), len(str2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
max_len = 0
# 填充dp数组
for i in range(1, m + 1):
for j in range(1, n + 1):
if str1[i-1] == str2[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
max_len = max(max_len, dp[i][j])
return max_len
while True:
try:
str1 = input().strip()
str2 = input().strip()
print(longest_common_substring(str1, str2))
except:
break
import java.util.*;
public class Main {
public static int longestCommonSubstring(String str1, String str2) {
int m = str1.length(), n = str2.length();
int[][] dp = new int[m + 1][n + 1];
int maxLen = 0;
// 填充dp数组
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (str1.charAt(i-1) == str2.charAt(j-1)) {
dp[i][j] = dp[i-1][j-1] + 1;
maxLen = Math.max(maxLen, dp[i][j]);
}
}
}
return maxLen;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = sc.nextLine();
String str2 = sc.nextLine();
System.out.println(longestCommonSubstring(str1, str2));
}
}
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int longestCommonSubstring(string str1, string str2) {
int m = str1.length(), n = str2.length();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
int maxLen = 0;
// 填充dp数组
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (str1[i-1] == str2[j-1]) {
dp[i][j] = dp[i-1][j-1] + 1;
maxLen = max(maxLen, dp[i][j]);
}
}
}
return maxLen;
}
int main() {
string str1, str2;
while (getline(cin, str1) && getline(cin, str2)) {
cout << longestCommonSubstring(str1, str2) << endl;
}
return 0;
}
算法分析
- 算法:动态规划
- 时间复杂度: O ( m × n ) \mathcal{O}(m \times n) O(m×n),其中 m m m 和 n n n 是两个字符串的长度
- 空间复杂度: O ( m × n ) \mathcal{O}(m \times n) O(m×n),需要二维dp数组

被折叠的 条评论
为什么被折叠?



