版本比较
code
正文
增加 61 行删除 172 行
1232345675897891011121314151617181011121314151617182019202223242526272829303132333435363738394041424344454647484950512122535455565758596061232463252627282966303132336834353637383970404142724344454647484950515253545556575859606162637677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141156465666768691187071727374120121122123124125126127128129757677787913413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319480818283848586```cpp#include <bits/stdc++.h>using namespace std;#include <algorithm>#include <cmath>#include <cstring>#include <iostream>#include <vector>
struct STvoid Solve(){ std::vector<std::vector<int>> st; ST(std::string &s) { int rge = (int)(s.size()); int log = std::log2(rge) + 1; st.assign(s.size(), std::vector<int>(log + 1, 0)); for (int i = 0; i < rge; i++) st[i][0] = s[i] - 'a'; for (int i = 1; (1 << i) <= rge; i++) for (int j = 0; j + (1 << i) - 1 < rge; j++) st[j][i] = std::min(st[j][i - 1], st[j + (1 << (i - 1))][i - 1]); } std::string s, t; std::cin >> s; s = '#' + s; std::vector<std::vector<int>> pre(s.size(), std::vector<int>(26, 0)); pre[1][s[1] - 'a'] = 1; for (int j = 2; j < s.size(); j++) for (int i = 0; i < 26; i++) pre[j][i] = pre[j - 1][i] + ((s[j] - 'a') == i);
int Query(int l, int r) for (int i = 1, j = s.size() - 1; i <= j;) { int len = std::log2(r - l + 1); return std::min(st[l][len], st[r - (1 << len) + 1][len]); }};
// Your codeclass YourCode{ public: static string Solve(const string &input) {
}};
// Solution codeclass SolutionCode{ private: static const int maxn = 5005; int pre[maxn][26];
public: string solve(const string &input) { string s = input; int n = s.size(); s = " " + s; memset(pre[0], 0, sizeof(pre[0])); for (int i = 1; i < (int)s.size(); i++) if (s[i] == s[j]) // 要判断 { memcpy(pre[i], pre[i - 1], sizeof(pre[i])); pre[i][s[i] - 'a']++; }
string ans; int l = 1, r = n; while (l <= r) { if (l == r) if (i == j || i + 1 == j) // 到底了 { ans += s[l]; t.push_back(s[i]); if (i + 1 == j) t.push_back(s[i]); break; } if (s[l] < s[r])
int l = i, r = j, resl = i, resr = j; // 得到左边最后一个相同字符的位置 while (l <= r) { ans += s[l++]; int mid = (l + r) >> 1; if (pre[mid][s[i] - 'a'] - pre[i][s[i] - 'a'] == mid - i) resl = mid, l = mid + 1; else r = mid - 1; } else if (s[l] > s[r]) l = i, r = j; while (l <= r) // 得到右边最后一个相同字符的位置 { ans += s[r--]; int mid = (l + r) >> 1; if (pre[j - 1][s[j] - 'a'] - pre[mid - 1][s[j] - 'a'] == j - mid) resr = mid, r = mid - 1; else l = mid + 1; }
if (resl > resr) // 特判全是同一种字符 { t.push_back(s[i]); i++, j--; continue; }
if (s[resl + 1] <= s[i] && s[resr - 1] <= s[j]) // 内部都更小,两边推进 { t.push_back(s[i]); i++, j--; } else { if (l == r - 1) { ans += s[l]; ans += s[l]; break; } int ll = l, rr = r, posl = l; while (ll <= rr) { int mid = (ll + rr) >> 1; if (pre[mid][s[l] - 'a'] - pre[l][s[l] - 'a'] == mid - l) posl = mid, ll = mid + 1; else rr = mid - 1; } int posr = r; ll = l, rr = r; while (ll <= rr) { int mid = (ll + rr) >> 1; if (pre[r - 1][s[r] - 'a'] - pre[mid - 1][s[r] - 'a'] == r - mid) posr = mid, rr = mid - 1; else ll = mid + 1; } if (posl > posr) { ans += s[l++]; r--; continue; } if (s[posl + 1] <= s[posl] && s[posr - 1] <= s[posr]) ans += s[l++], r--; else { if (s[posl + 1] < s[posr - 1]) ans += s[l++]; else ans += s[r--]; } if (s[resl + 1] < s[resr - 1]) // 优先开发更短的 t.push_back(s[i]), i++; else // 特别的,如果内部都更大,随便选一种方案 t.push_back(s[j]), j--; } } return ans; else if (s[i] < s[j]) // 必然的贪心 t += s[i], i++; else t += s[j], j--; }};
string generateRandomString(int n){ string s; for (int i = 0; i < n; i++) { s += char('a' + rand() % 26); } return s; std::cout << t << '\n';}
int main(){ srand(time(0));
SolutionCode solution; YourCode yourCode;
int totalTests = 0; int found = 0; string errorString, correctOutput, yourOutput;
cout << "Starting tests...\n";
// Test small random strings for (int test = 1; test <= 100000 && found == 0; test++) { // Generate random string of length 1-10 int n = 1 + rand() % 6; string s = generateRandomString(n);
// Run solution code string correct = solution.solve(s);
// Run your code string yours = yourCode.Solve(s);
totalTests++;
if (correct != yours) { errorString = s; correctOutput = correct; yourOutput = yours; found = 1; break; }
if (test % 1000 == 0) { cout << "Tested " << test << " cases...\n"; } }
cout << "\n=== Results ===\n"; cout << "Total tests run: " << totalTests << "\n";
if (found) { cout << "\n\Found a counterexample!\n"; cout << "Input: \"" << errorString << "\"\n"; cout << "Correct output: \"" << correctOutput << "\"\n"; cout << "Your output: \"" << yourOutput << "\"\n"; cout << "String length: " << errorString.size() << "\n";
cout << "\n=== Test case for file ===\n"; cout << "1\n"; cout << errorString << "\n"; } else { cout << "\n✅ All tests passed!\n"; }
int T; std::cin >> T; while (T--) Solve(); return 0;}```