LCY进阶 week1 STL

T1链接

题意:把两个集合的元素全部凑到一个集合中,从小到大输出即可;
idea:用set天然带有去重的功能

ACcode:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#define LL long long
using namespace std;

int n,m,t;

int main()
{
	while( scanf("%d %d",&n,&m)!=EOF )
	{
		set<int> s;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&t);
			s.insert(t);
		}
		for(int i=1;i<=m;i++)
		{
			scanf("%d",&t);
			s.insert(t);
		}
		int i=0;
		while( s.size()>0 )
		{
			if( i==0 ) printf("%d",( *s.begin() ));
			else printf(" %d",( *s.begin() ));
			s.erase( s.begin() );
			i++;
		}
		cout<<endl;
	}
	return 0;
}

T2链接

题意:给定火车的进站顺序和出栈顺序,问能否做到按进站顺序进然后按出站顺序出去。

idea:栈的经典题了,遍历进站顺序,每次压进去前检查栈顶的元素是否和当前匹配的出栈的车号一样,一样就出栈,直到没有可以出栈的了,再压进去。

PS:输入一定是按字符来输入

ACcode:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#define LL long long
#define N 1010
using namespace std;

int n,in[N],out[N],ans[N],len=0;

int main()
{
//	freopen("in.txt","r",stdin);
	while( scanf("%d",&n)!=EOF )
	{
		len = 0;
		char c;
		int j;
		bool pd = true;
		for(int i=1;i<=n;i++)
		{
			cin>>c;
			in[i] = c-'0';
		}
		for(int i=1;i<=n;i++)
		{
			cin>>c;
			out[i] = c-'0';
			
		}
		stack<int> s;
		j = 1;
		for(int i=1;i<=n;i++)
		{
			while( !s.empty() && out[j]==s.top() )
			{
				j++;
				s.pop();
				ans[++len] = 2;
			}
			s.push( in[i] );
			ans[++len] = 1;
		}
		while( j<=n && !s.empty() && out[j]==s.top() )
		{
			ans[++len] = 2;
			j++;
			s.pop();
		}
		if( !s.empty() ) pd = false;
		
		if( pd ) printf("Yes.\n");
		else printf("No.\n");
		
		if( pd ) 
		for(int i=1;i<=len;i++)
		{
			if( ans[i]==1 ) printf("in\n");
			else if(ans[i]==2) printf("out\n");
		}
		printf("FINISH\n");
	}
	return 0;
}

T3 链接

题意:问一堆元素里面剔除重复的元素外一共有几个
iead:set乱搞

ACcode:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#define LL long long
using namespace std;

int n,ans;
string s;
set<string> sa;

int main()
{
	while( scanf("%d",&n)!=EOF )
	{
		sa.clear();
		for(int i=1;i<=n;i++)
		{
			cin>>s;
			sa.insert(s);
		}	
		printf("%d\n",sa.size());
	}
	return 0;
}

T4链接

题意:一个从小到大排好序的数组,第M字典序小的序列是什么。
idea:STL中的next_permutation(取下一个更大的序列)和prev_permutation(取上一个更小的序列)

ACcode:

#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#define LL long long
#define N 1010
using namespace std;

int n,m,a[N];

int main()
{
//	freopen("in.txt","r",stdin);
	while( scanf("%d %d",&n,&m)!=EOF )
	{
		for(int i=1;i<=n;i++) a[i] = i;
		for(int i=1;i<=m-1;i++)
		{
			next_permutation( a+1,a+n+1 );
		}
		printf("%d",a[1]);
		for(int i=2;i<=n;i++) printf(" %d",a[i]);
		cout<<endl;
	}
	return 0;
}

T5链接

题意:给定一个火星词汇对应的英文,再给几句火星文+英文的句子,要你翻译成全英文。

idae:用map,而且没有给出的词汇直接输出就是(本来就是英文)

PS:注意用getline前的getchar

ACcode:

#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#define LL long long
#define N 1010
using namespace std;

map<string,string>ma;
map<string,string>::iterator it;
string a,b,s;
int n,len;
bool check(char c)
{
	if( ( c>='a'&&c<='z' ) || ( c>='A'&&c<='Z' ) ) return true;
	else return false;
}

int main()
{
//	freopen("in.txt","r",stdin);
	cin>>a;
	while( cin>>a )
	{
		if( a.compare("END")==0 ) break;
		else cin>>b;
		ma[b] = a;
	}
	cin>>a;
	getchar();
	while( 1 )
	{
		getline(cin,s);
		if( s=="END" ) break;
		n = s.size();
		for(int i=0;i<n;i++)
		{
			len = 0;
			char d[1010];
			if( check(s[i]) )
			{
				len = 0;
				for(;i<n;i++)
				{
					if( check(s[i]) ) d[len++] = s[i];
					else break;
				}
				d[len] = '\0';
				if( ma.find(d)!=ma.end() ) cout<<ma[d];
				else cout<<d;
			}
			cout<<s[i];
		}
		cout<<endl;
	}
	return 0;
}

T6 链接

题意:括号匹配…
idea:用栈或者直接扫一遍,遇到’ } ’ 但是没有对应的’ { ’ 了就ans++(把此右括号变成左括号),最后加上还剩下的 左括号数/2就是答案。

ACcode:

#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#define LL long long
#define N 1010
using namespace std;

string s;
int l,h=0,ans;

int main()
{
//	freopen("in.txt","r",stdin);
	while( cin>>s )
	{
		ans = 0;
		l = 0;
		if( s[0]=='-' ) break;
		h++;
		cout<<h<<". ";
		for(int i=0;i<s.size();i++)
		{
			
		if( s[i]=='{' )
		{
			l++;
		}
		else 
		{
			if( l>0 )
			{
				l--;
			}
			else
			{
				ans++;
				l++;
			}
		}
			
		}
		ans += l/2;
		cout<<ans<<endl;
	}
	return 0;
}
上一篇:Andrew Ng 神经网络与深度学习 week1


下一篇:数据库系统week1-2——2021.9.17