QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#676947 | #3259. Substring Characters | bigboidan | WA | 0ms | 3580kb | C++14 | 3.4kb | 2024-10-26 04:34:13 | 2024-10-26 04:34:14 |
Judging History
answer
#include <iostream>
#include <unordered_map>
#include <set>
#include <string>
using namespace std;
int main() {
string in;
int inputNum = 1;
//-------------------------------------------------------------------
// Actual algorihtm starts here
// 104001144 : {1,0,4}
// 104
// 4001
// 0114
// Output should be 3
while (cin >> in) {
// Top line just inputting from input file
// To setup the amount of unique chars necessary for the minimal substring
set<char> allChars(in.begin(), in.end());
// To keep track of items in current window
unordered_map<char, int> curWin;
// To check if the substring/window we found already existed in our set (must be unique)
unordered_map<string, bool> solutions;
// Left pointer collapses (l), right pointer expands (r), occur is amount of unique substrings
int l = 0, r = 0, uniqueSubstrings = 0;
// For handling the final iteration, fix loop conditions instead
// Find way to run logic on last insertion. Done I think
// Expand list rightwards until we hit end. Care on the final element.
while (r < in.length()) {
// Check if the element on the right pointer is in the window yet
// WHY ARE WE CHECKING FOR SUBSTRINGS AFTER CONFIRMING EXISTENCE??????
// SHOULD BE CHECKING UPON INSERTION!
// Check if the rightmost element we just added satisfies our window
// If so, run the collapse logic
if(curWin.find(in[r]) == curWin.end()){
// Wasn't in the list so insert it and check if window is valid
curWin[in[r]]++;
// Inserted. Now check if hte list is of the correct size
if(allChars.size() == curWin.size()){
// We now have all valid elements.
// Collapse l and see what's going on. Get our minimal substring
while(l<=r){
if(curWin[in[l]] > 1){
curWin[in[l++]]--;
continue;
}
break; // Removing element leaves us in shambles
}
// If the right pointer is the last element, and the first element is 0th,
// doesnt work
if(l==0 && r==in.length()-1) break;
// Now check our substring set to see if this is new and unique
if(solutions.find(in.substr(l, r-l)) == solutions.end()){
// Unique. Increment counter and insert
//cout << r-l << ": " << in.substr(l, r-l+1) << endl;
solutions[in.substr(l, r-l)] = true;
uniqueSubstrings++;
}
// Now move left index up one and remove from window
curWin.erase(in[l++]);
}
}
else {
// So character was found in the string. Just increment and keep moving
curWin[in[r]]++;
}
r++;
}
//---------------------------------------------------------------
// Outputting results of each input. Not part of algorithm
cout << "Input" << inputNum<< ": " << uniqueSubstrings << endl;
inputNum++;
}
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3580kb
input:
aabbabb abAB34aB3ba7 104001144 aaabcaaa a bb bd 1234567
output:
Input1: 2 Input2: 1 Input3: 3 Input4: 2 Input5: 0 Input6: 1 Input7: 0 Input8: 0
result:
wrong answer 1st lines differ - expected: '2', found: 'Input1: 2'