60642023-10-29 11:42:43GervidForgó rulettkerékcpp17Time limit exceeded 35/1001.565s5904 KiB
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n, m, i, j, k, x;
    cin >> n >> m;

    vector<string> patterns(1);
    patterns.reserve(n);

    cin >> patterns[0];

    vector<int> count(1, 1);
    count.reserve(n);

    string tag;

    for (i = 1; i < n; i++)
    {
        cin >> tag;
        x = 0, k = 0;

        for (j = 0; j < patterns.size(); j++)
        {
            for (k = 0; k < m; k++)
            {
                while (x < m && (x + k < m && tag[x+k] == patterns[j][x]) || (x + k >= m && tag[x + k - m] == patterns[j][x]))
                {
                    x++;
                }
            
                if (x == m)
                {
                    count[j]++;
                    break;
                }

                x = 0;
            }
            if (x == m)
            {
                break;
            }
        }

        if (x != m)
        {
            patterns.push_back(tag);
            count.push_back(1);
        }
    }

    int sum = 0;

    for (i = 0; i < count.size(); i++)
    {
        if (count[i] > 1)
        {
            sum += (count[i] * (count[i]-1)) >> 1;
        }
    }

    cout << sum;
}
//4 4
//abcd
//xbcd
//cdab
//dabc
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1684 KiB
2Accepted3ms1932 KiB
subtask220/20
3Accepted2ms2008 KiB
4Accepted3ms2056 KiB
5Accepted3ms2276 KiB
6Accepted3ms2488 KiB
7Accepted6ms2740 KiB
8Accepted3ms2956 KiB
9Accepted3ms3180 KiB
subtask315/15
10Accepted25ms3412 KiB
11Accepted43ms3460 KiB
12Accepted126ms3516 KiB
13Accepted32ms3508 KiB
14Accepted52ms3844 KiB
15Accepted626ms4196 KiB
16Accepted670ms4592 KiB
17Accepted1.014s4496 KiB
subtask40/65
18Accepted182ms4228 KiB
19Accepted523ms4416 KiB
20Accepted432ms5236 KiB
21Accepted1.243s5288 KiB
22Accepted554ms4872 KiB
23Accepted134ms4816 KiB
24Time limit exceeded1.557s4384 KiB
25Accepted303ms5220 KiB
26Accepted377ms4924 KiB
27Time limit exceeded1.526s4640 KiB
28Time limit exceeded1.565s5068 KiB
29Time limit exceeded1.514s5904 KiB
30Time limit exceeded1.557s5100 KiB