Submission #46561251


Source Code Expand

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Tasks;

public class E
{
    public static void Main()
    {
        using var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        Solve();
        Console.Out.Flush();
    }

    public static void Solve()
    {
        var (N, T) = Scanner.Scan<int, string>();
        var S = new string[N];
        for (var i = 0; i < N; i++)
        {
            S[i] = Scanner.Scan<string>();
        }

        var A = new int[N];
        var B = new int[N];
        for (var i = 0; i < N; i++)
        {
            var a = 0;
            var b = 0;
            for (var j = 0; j < S[i].Length; j++)
            {
                if (a < T.Length && S[i][j] == T[a]) a++;
                if (b < T.Length && S[i][^(j + 1)] == T[^(b + 1)]) b++;
            }

            A[i] = a;
            B[i] = b;
        }

        Array.Sort(B);

        long answer = 0;
        for (var i = 0; i < N; i++)
        {
            var lb = LowerBound(B, T.Length - A[i]);
            answer += N - lb;
        }

        Console.WriteLine(answer);
    }

    public static int LowerBound<T>(List<T> source, T key, IComparer<T>? comparer = null)
        => LowerBound(System.Runtime.InteropServices.CollectionsMarshal.AsSpan(source), key, comparer);

    public static int LowerBound<T>(ReadOnlySpan<T> source, T key, IComparer<T>? comparer = null)
    {
        comparer ??= Comparer<T>.Default;
        var (lo, hi) = (-1, source.Length);
        while (hi - lo > 1)
        {
            var mi = lo + ((hi - lo) >> 1);
            if (comparer.Compare(source[mi], key) >= 0) hi = mi;
            else lo = mi;
        }

        return hi;
    }

    public static class Scanner
    {
        public static T Scan<T>() where T : IConvertible => Convert<T>(ScanStringArray()[0]);
        public static (T1, T2) Scan<T1, T2>() where T1 : IConvertible where T2 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]));
        }
        public static (T1, T2, T3) Scan<T1, T2, T3>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]));
        }
        public static (T1, T2, T3, T4) Scan<T1, T2, T3, T4>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]), Convert<T4>(input[3]));
        }
        public static (T1, T2, T3, T4, T5) Scan<T1, T2, T3, T4, T5>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]), Convert<T4>(input[3]), Convert<T5>(input[4]));
        }
        public static (T1, T2, T3, T4, T5, T6) Scan<T1, T2, T3, T4, T5, T6>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible where T6 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]), Convert<T4>(input[3]), Convert<T5>(input[4]), Convert<T6>(input[5]));
        }
        public static IEnumerable<T> ScanEnumerable<T>() where T : IConvertible => ScanStringArray().Select(Convert<T>);
        private static string[] ScanStringArray()
        {
            var line = Console.ReadLine()?.Trim() ?? string.Empty;
            return string.IsNullOrEmpty(line) ? Array.Empty<string>() : line.Split(' ');
        }
        private static T Convert<T>(string value) where T : IConvertible => (T)System.Convert.ChangeType(value, typeof(T));
    }
}

Submission Info

Submission Time
Task E - Joint Two Strings
User AconCavy
Language C# 11.0 (.NET 7.0.7)
Score 500
Code Size 4226 Byte
Status AC
Exec Time 190 ms
Memory 71488 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 4
AC × 44
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt, example3.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, example0.txt, example1.txt, example2.txt, example3.txt
Case Name Status Exec Time Memory
000.txt AC 175 ms 69148 KiB
001.txt AC 172 ms 69276 KiB
002.txt AC 38 ms 26444 KiB
003.txt AC 39 ms 26624 KiB
004.txt AC 37 ms 26472 KiB
005.txt AC 38 ms 26492 KiB
006.txt AC 46 ms 30268 KiB
007.txt AC 44 ms 30148 KiB
008.txt AC 39 ms 25308 KiB
009.txt AC 190 ms 69132 KiB
010.txt AC 68 ms 34876 KiB
011.txt AC 90 ms 43548 KiB
012.txt AC 45 ms 27952 KiB
013.txt AC 49 ms 28428 KiB
014.txt AC 68 ms 34724 KiB
015.txt AC 42 ms 26464 KiB
016.txt AC 45 ms 27232 KiB
017.txt AC 42 ms 26396 KiB
018.txt AC 45 ms 26640 KiB
019.txt AC 41 ms 26708 KiB
020.txt AC 44 ms 26416 KiB
021.txt AC 41 ms 26568 KiB
022.txt AC 39 ms 27156 KiB
023.txt AC 40 ms 26624 KiB
024.txt AC 35 ms 27428 KiB
025.txt AC 44 ms 27312 KiB
026.txt AC 41 ms 27584 KiB
027.txt AC 39 ms 27564 KiB
028.txt AC 46 ms 27392 KiB
029.txt AC 48 ms 27528 KiB
030.txt AC 49 ms 27212 KiB
031.txt AC 37 ms 27372 KiB
032.txt AC 50 ms 27580 KiB
033.txt AC 42 ms 27296 KiB
034.txt AC 42 ms 26996 KiB
035.txt AC 39 ms 27668 KiB
036.txt AC 42 ms 27180 KiB
037.txt AC 182 ms 71420 KiB
038.txt AC 179 ms 71488 KiB
039.txt AC 182 ms 71480 KiB
example0.txt AC 31 ms 25224 KiB
example1.txt AC 38 ms 25292 KiB
example2.txt AC 41 ms 25416 KiB
example3.txt AC 35 ms 25304 KiB