解题思路
需要判断字符串是否满足三个条件:
- 全是大写字母
- 没有连续相等的字母
- 没有形如"xyxy"的子序列
关键点
- 检查连续字母比较简单,只需要遍历一次
- 检查"xyxy"形式的子序列需要考虑不连续的情况
- 任何一个条件不满足就不喜欢
代码
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
// 检查是否有连续相同字母
bool hasConsecutive(const string& s) {
for (int i = 1; i < s.length(); i++) {
if (s[i] == s[i-1]) return true;
}
return false;
}
// 检查是否有xyxy形式的子序列
bool hasXYXYSubsequence(const string& s) {
int n = s.length();
// 枚举所有可能的x和y的位置
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// 找到第一个xy
char x = s[i], y = s[j];
// 在j之后寻找第二个xy
int found_x = -1;
for (int k = j + 1; k < n; k++) {
if (s[k] == x) {
found_x = k;
break;
}
}
if (found_x != -1) {
for (int k = found_x + 1; k < n; k++) {
if (s[k] == y) {
return true;
}
}
}
}
}
return false;
}
public:
string checkWord(const string& s) {
// 检查是否全是大写字母
for (char c : s) {
if (c < 'A' || c > 'Z') {
return "Dislikes";
}
}
// 检查连续相同字母
if (hasConsecutive(s)) {
return "Dislikes";
}
// 检查xyxy形式的子序列
if (hasXYXYSubsequence(s)) {
return "Dislikes";
}
return "Likes";
}
};
int main() {
string s;
cin >> s;
Solution solution;
cout << solution.checkWord(s) << endl;
return 0;
}
import java.util.Scanner;
public class Main {
static class Solution {
// 检查是否有连续相同字母
private boolean hasConsecutive(String s) {
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == s.charAt(i-1)) return true;
}
return false;
}
// 检查是否有xyxy形式的子序列
private boolean hasXYXYSubsequence(String s) {
int n = s.length();
// 枚举所有可能的x和y的位置
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// 找到第一个xy
char x = s.charAt(i), y = s.charAt(j);
// 在j之后寻找第二个xy
int found_x = -1;
for (int k = j + 1; k < n; k++) {
if (s.charAt(k) == x) {
found_x = k;
break;
}
}
if (found_x != -1) {
for (int k = found_x + 1; k < n; k++) {
if (s.charAt(k) == y) {
return true;
}
}
}
}
}
return false;
}
public String checkWord(String s) {
// 检查是否全是大写字母
for (char c : s.toCharArray()) {
if (c < 'A' || c > 'Z') {
return "Dislikes";
}
}
// 检查连续相同字母
if (hasConsecutive(s)) {
return "Dislikes";
}
// 检查xyxy形式的子序列
if (hasXYXYSubsequence(s)) {
return "Dislikes";
}
return "Likes";
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
Solution solution = new Solution();
System.out.println(solution.checkWord(s));
sc.close();
}
}
class Solution:
def has_consecutive(self, s: str) -> bool:
"""检查是否有连续相同字母"""
for i in range(1, len(s)):
if s[i] == s[i-1]:
return True
return False
def has_xyxy_subsequence(self, s: str) -> bool:
"""检查是否有xyxy形式的子序列"""
n = len(s)
# 枚举所有可能的x和y的位置
for i in range(n):
for j in range(i + 1, n):
# 找到第一个xy
x, y = s[i], s[j]
# 在j之后寻找第二个xy
found_x = -1
for k in range(j + 1, n):
if s[k] == x:
found_x = k
break
if found_x != -1:
for k in range(found_x + 1, n):
if s[k] == y:
return True
return False
def check_word(self, s: str) -> str:
# 检查是否全是大写字母
if not s.isupper():
return "Dislikes"
# 检查连续相同字母
if self.has_consecutive(s):
return "Dislikes"
# 检查xyxy形式的子序列
if self.has_xyxy_subsequence(s):
return "Dislikes"
return "Likes"
def main():
s = input().strip()
solution = Solution()
print(solution.check_word(s))
if __name__ == "__main__":
main()
算法及复杂度
- 算法:暴力枚举
- 时间复杂度:,其中 是字符串长度
- 检查连续字母:
- 检查xyxy子序列:
- 空间复杂度:,只使用了常数额外空间