QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#676948#3259. Substring CharactersfazlavarxAC ✓1ms3588kbC++142.4kb2024-10-26 04:36:292024-10-26 04:36:29

Judging History

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

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

answer

#include <iostream>
#include <set>
#include <unordered_map>

using namespace std;

int main()
{
    string input = "";
    while (cin >> input)
    {
        int l = 0, r = 0;
        set<char> Char(input.begin(), input.end()); // set for size
        unordered_map<char, int> occ;               // hashmap check how many times char occurs
        unordered_map<string, int> Appeared;        // hashmap to keep track of repeating substrings

        int s = Char.size(); // value to compare to
        int curr = 0;        // how many unique chars are within window
        int occurance = 0;   // return value
        while (l <= r)
        {
            // if we reached end, break
            if (r == input.length())
                break;
            // if we haven't seen char yet, increment unique curr within sliding window
            if (occ[input[r]] == 0)
                curr++;

            occ[input[r]]++; // increment occurence within occ hashmap

            if (curr == s)
            {
                // check left pointer
                // if char already exists within window, increment l
                while (occ[input[l]] > 1)
                {
                    occ[input[l]]--;
                    l++;
                }
                // put the substring within Appeared hashmap to keep track of substrings
                string sub = "";
                for (int i = l; i <= r; i++)
                {
                    sub += input[i];
                }

                // cout << sub << endl;

                Appeared[sub]++;

                // if it appears more then once, dont increment occurances
                if (Appeared[sub] == 1)
                {
                    if (input.length() != curr)
                    {
                        occurance++;

                        // we have optimal window
                        curr--;
                        occ[input[l]] = 0;
                        l++;
                    }
                    // we breakout if the valid substring is the size of string
                    else
                        break;
                }
                else
                {
                    curr--;
                    occ[input[l]] = 0;
                    l++;
                }
            }
            r++;
        }
        cout << occurance << endl;
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3588kb

input:

kejfASKDNF2342389fjsl2lsklsdflkjdKDFJKFD2342359829jdkjfkdklsjekfw234234234235123
11111111111111111111111111111111111111111111111111111111111111111111111111111111
12312343123123142312341234123412341234123423412312341234123232312341232341234123

output:

1
1
11

result:

ok 3 lines

Test #3:

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

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: 3528kb

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: 0ms
memory: 3512kb

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