QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#676949#3259. Substring CharactersbigboidanAC ✓1ms3764kbC++143.3kb2024-10-26 04:36:342024-10-26 04:36:36

Judging History

你现在查看的是最新测评结果

  • [2024-10-26 04:36:36]
  • 评测
  • 测评结果:AC
  • 用时:1ms
  • 内存:3764kb
  • [2024-10-26 04:36:34]
  • 提交

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 << uniqueSubstrings << endl;
        inputNum++;
	}
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3636kb

input:

aabbabb
abAB34aB3ba7
104001144
aaabcaaa
a
bb
bd
1234567

output:

2
1
3
2
0
1
0
0

result:

ok 8 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3608kb

input:

kejfASKDNF2342389fjsl2lsklsdflkjdKDFJKFD2342359829jdkjfkdklsjekfw234234234235123
11111111111111111111111111111111111111111111111111111111111111111111111111111111
12312343123123142312341234123412341234123423412312341234123232312341232341234123

output:

1
1
11

result:

ok 3 lines

Test #3:

score: 0
Accepted
time: 1ms
memory: 3576kb

input:

1234543212345432123454321234543212345432123454321234543212345432123454321
1234543212345432123454321234543212345432123454321234543212345432123454321Z
a1234543212345432123454321234543212345432123454321234543212345432123454321
ABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCD...

output:

2
1
1
6
1
1
25
2
1
1
1
0
0
1
1
4
5
2
1
1
1
1

result:

ok 22 lines

Test #4:

score: 0
Accepted
time: 1ms
memory: 3524kb

input:

0123456789ABCDEFEDCBA9876543210123456789ABCDEFEDCBA98765432123456789ABCDEFEDCBA
abcdeadbedcacdebadecbdaecdbacebdcaebecadebacebdabecdcbeabedcebdadbcecbdaedbcacb
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcbaqdKYJt2iHEL3vWFghr
99RBKQEBTBCA8WRSVNBSCGVZFLPTP7F29965694YAKW3B77CCFXCNEWRRL8...

output:

2
32
1
2
1
2
2
1
2
1
1
8
7
8
13
1
6
3
2
1
1
1

result:

ok 22 lines

Test #5:

score: 0
Accepted
time: 1ms
memory: 3764kb

input:

x
dddddddgggggg
dgdgdgdgdgdg
ef1ef1e
ef1ef1
abcbacbacab
cabcbacbacabc
kadksfkaddkdfakkjdfjdfasjdafhgdafsdasflalsdlkhdfhdasjklsadkjdafhklsajfghdasjkdg
1234567890
1234567890987654321
ssss
aba
ec26hb6a4l2wv3rm1ut7r9nxtejs5riidbxenodgipyjjuxtdwi3bykyhnauvv5gvws8r7fxdady7ek8
q3g12voqn3mewezr1jfoejpgh4vc4...

output:

0
1
2
3
3
5
5
6
0
2
1
2
1
1
1
1
1
1
1
7
4
1

result:

ok 22 lines