3 solutions

  • 0
    @ 2022-3-10 23:25:45

    用map的做法之一,比较绕,但还是很好理解的

    #include<iostream>
    #include<map>
    #include<cstring>
    using namespace std;
    const int N=117;
    int n,ans;
    map<string,int>mp;
    struct A{
    	char x[57];
    	bool v;//用于排除重复加相同字符串的情况
    }a[N];
    int main(){
    	cin>>n;
    	for(int i=0;i<n;i++){
    		cin>>a[i].x;
    		mp[a[i].x]=1;
    		a[i].v=1;
    		for(int j=0;j<i;j++){
    			int flag=0; 
    			int k=strlen(a[j].x);
    			for(int l=0;l<k;l++)
    				if(a[j].x[l]!=a[i].x[l])flag=1;		
    			if(!flag&&k!=strlen(a[i].x)&&a[j].v){
    				mp[a[i].x]++;
    			}
    			ans=max(mp[a[i].x],ans);
    			if(!flag&&k==strlen(a[i].x))a[j].v=0;
    		}
    	}
    	cout<<ans;
    	return 0;
    }
    
    • 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;
      }
      
      • 0
        @ 2022-1-11 19:12:22
        #include <bits/stdc++.h>
        using namespace std;
        string s[107];
        int ss[107];
        int main(){
        	int n;
        	cin>>n;
        	for(int i=0;i<n;i++){
        		cin>>s[i];
        	}
        	for(int i=0;i<n;i++){
        		for(int j=i-1;j>=0;j--){
        			if(s[i].find(s[j])!=string::npos&&s[i]!=s[j]){
        				ss[i]=ss[j]+1;
        				break;
        			}
        		}
        	}
        	int maxx=0;
        	for(int i=1;i<n;i++){
        		maxx=max(maxx,ss[i]);
        	}
        	cout<<maxx+1;
        	return 0;
        }
        
        • 1

        Information

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