美团点评2020校招测试方向笔试题

一:
直接输出, 用flag记录是否有答案,一次循环,时间复杂度O(n)。
代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

string s; 

int main()
{
    
    
	while(cin >> s)
	{
    
    
		if(s.size() == 1)
		{
    
    
			cout << s << endl;
			continue;
		}
		bool flag = false;
		for(int i = 0; i < s.size() - 1; i++)
		{
    
    
			if(s[i] != s[i + 1])
			{
    
    
				cout << s[i];
				flag = true;
			}		
			else
			{
    
    
				char t = s[i];
				while(s[i] == t)
					i++;
				i--;
			}
		}
		if(!flag)
			cout <<"no" << endl;
		else
		{
    
    
			if(s[s.size() - 1] != s[s.size() - 2])
				cout << s[s.size() - 1] <<endl;
			else cout << endl;
		}
		s = "";
	}

	return 0;
}

二:
字符串枚举。
代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

string s1, s2; 

string getMinString(string str1, string str2) 
{
    
    
          string ans;
      if(str2.size() == 0)
             return str1;
       for(int i = 0; i < str1.size(); i++)
       {
    
    
       		int a[110];
    		for(int i = 0; i <= str2.size(); i++)
    			a[i] = 1;
    			
       		int tol = str2.size();
       		
       		for(int j = i; j >= 0; j--)
       		{
    
    
       			for(int k = 0; k < str2.size(); k++)
       			{
    
    
       				if(str2[k] == str1[j] && a[k] == 1)
					{
    
    
						
						a[k]--;	
						tol--;  
						break;	
					}	
				}
				if(tol == 0)
				{
    
    
					
					string t = str1.substr(j, i - j + 1);
					//cout << t << endl;
					if(t.size() < ans.size() || ans.size() == 0)
						ans = t;
				}
			}
	   }
	   return ans;
}

int main()
{
    
    
	cin >> s1 >> s2;
	cout << getMinString(s1, s2) << endl;

	return 0;
}

三:

交叉输出就行
代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 1000010;

int b[N], g[N];
int n;

int main()
{
    
    
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> b[i];
	for(int i = 0; i < n; i++)
		cin >> g[i];
		
	for(int i = 0; i < n; i++)
	{
    
    
		cout << b[i] << endl;	
		cout << g[i] << endl;
	}	

	return 0;
}

四:
暴力枚举

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 1000010;

int ax[30][30];

int main()
{
    
    
	char c;
	int col = 0, row = 0;
	int j;
    char last;
    int k = 1;
    bool change = false;
	while(cin >> c)
	{
    
    
		//cout << c <<endl;
		if(k == 1)
		{
    
    
			k++;
			continue;
		 } 
		if(c == '}' && last == '}')
			break;
		if(c == '{')
		{
    
    
			row++;
			j = 1;
	
		}
		else if(c == '1')
		{
    
    
			ax[row][j] = 1;
			col = max(col, j);
			j++;
			
		}
		else if(c == '0')
		{
    
    
			ax[row][j] = 0;
			col = max(col, j);
			j++;
		}
		
        last = c;
	}

	int ans = 0;
	for(int a = 1; a <= row; a++)
	  for(int b = 1; b <= col; b++)
		for(int c = 1; c <= a; c++)
		  for(int d = 1; d <= b; d++)
		  {
    
    
		  	int t = 0;
		  	bool zore = true;
		  	for(int e = c; e <= a && zore; e++)
		  	  for(int g = d; g <= b && zore; g++)
		  	    if(ax[e][g] == 1 && zore)
		  	      t++;
		  	    else
		  	      zore = false;
		  	if(zore)
		  		ans = max(ans, t);
		  }
	cout << ans << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45432665/article/details/108102785