UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#212165#3818. 01游戏Panjunnan011ms1240kbC++1023b2024-10-13 11:44:452024-10-13 12:41:24

answer

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string s;
    cin >> s;
    int n = s.length();
    vector<int> dp(n, 1); // 初始化dp数组,每个位置至少可以形成长度为1的子段
    int ans = 1; // 答案至少为1

    // 动态规划求解
    for (int i = 1; i < n; i++) {
        if (s[i] == s[i-1]) {
            dp[i] = dp[i-1]; // 如果当前位与前一位相同,最长子段长度不变
        } else {
            dp[i] = dp[i-1] + 1; // 如果当前位与前一位不同,最长子段长度加1
        }
        ans = max(ans, dp[i]); // 更新答案
    }

   
    int max_value = 0;
    for (int i = 0; i < n; i++) {
        int value = 0;
 
        for (int j = i; j < n; j++) {
            if (s[j] != s[j-i-1]) {
                value = max(value, dp[j-i] + dp[i] - 1);
            }
        }
        max_value = max(max_value, value);
    }

    cout << max(ans, max_value) << endl;
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 1228kb

input:

11000001100100000010

output:

8

result:

wrong answer 1st numbers differ - expected: '9', found: '8'

Subtask #2:

score: 0
Wrong Answer

Test #9:

score: 0
Wrong Answer
time: 0ms
memory: 1228kb

input:

1101111111000101001010110010010111010010111001000100101001010110110001100110011101010010001101001100...

output:

168

result:

wrong answer 1st numbers differ - expected: '158', found: '168'

Subtask #3:

score: 0
Wrong Answer

Test #17:

score: 0
Wrong Answer
time: 11ms
memory: 1240kb

input:

0101100001001111011100100000011100110100001001100110111000100001110101010001010011000110001001110000...

output:

1010

result:

wrong answer 1st numbers differ - expected: '997', found: '1010'

Subtask #4:

score: 0
Time Limit Exceeded

Test #25:

score: 0
Time Limit Exceeded

input:

0001001111100010010111110001100000010001011100010011110001000101110010101011111100000101111100110100...

output:


result: