3 solutions

  • 0
    @ 2022-3-2 16:18:22

    栈的内存有点大

    #include <bits/stdc++.h>
    
    
    using namespace std;
    
    stack<string> ss;
    string str[100010];
    int cnt = 0;
    bool check(string a, string b)
    {
    	if (a == b) return false;
    	else {
    		int len = a.size();
    		for (int i = 0; i < len; i++)
    		{
    			if (a[i] != b[i]) return false;
    		}
    		return true;
    	}
    }
    
    int main(void)
    {
    	int n;
    	cin >> n;
    	for (int i = 0; i < n; i++) cin >> str[i];
    	sort(str, str + n);//a able i int inter
    	
    
    	for (int i = 0; i < n; i++)
    	{
    		if (ss.empty()) ss.push(str[i]);
    		else {
    			while (ss.size())
    			{
    				if (check(ss.top(), str[i]))
    				{
    					ss.push(str[i]);
    					break;
    				}
    				else ss.pop();
    			}
    			if (ss.empty()) ss.push(str[i]);
    		}
    		cnt = max(cnt, (int)ss.size());
    	}
    
    	cout << cnt;
    
    
    
    	return 0;
    }
    

    Information

    ID
    1099
    Time
    1000ms
    Memory
    16MiB
    Difficulty
    7
    Tags
    # Submissions
    48
    Accepted
    13
    Uploaded By