1 solutions

  • 4
    @ 2023-11-24 11:23:19

    @

    #include <iostream>
    using namespace std;
    
    #define MAXN 2000006
    
    using ll = long long;
    
    char ch[MAXN];
    
    int las[MAXN];
    int cnt[MAXN];
    
    int main()
    {
    	int n;
    	cin >> n;
    	ll res = 0;
    	for (int i = 1; i <= n; i++)
    	{
    		cin >> ch[i];
    		int j = i - 1;
    		while (j > 0 && ch[j] != ch[i])
    		{
    			j = las[j] - 1;
    		}
    		las[i] = j;
    		if (j > 0)
    		{
    			res += cnt[i] = cnt[j - 1] + 1;
    		}
    	}
    	cout << res << endl;
    	return 0;
    }
    
    
  • 1

Information

ID
9078
Time
1000ms
Memory
512MiB
Difficulty
5
Tags
# Submissions
4
Accepted
4
Uploaded By