A - ACboy needs your help again!
* 题意简单,模拟先进先出或者先进后出
* 栈 + 队列
* 但是这里我用的双向队列
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <deque>
using namespace std;
#define ll long long
#define ld long double
#define mm(a,b) memset(a,b,sizeof(a))
typedef pair<int,int> P;
int main(){
deque <int> dq;
int m,n,a;
scanf("%d",&m);
char s[10];
char ss[10];
while(m--){
scanf("%d %s",&n,s);
dq.clear();
while(n--){
if(strcmp(s, "FIFO")==0){
scanf("%s",ss);
if(strcmp(ss, "IN")==0){
scanf("%d",&a);
dq.push_front(a);
}
else{
if(dq.empty()){
cout<<"None"<<endl;
}
else{
cout<<dq.back()<<endl;
dq.pop_back();
}
}
}
else{
scanf("%s",ss);
if(strcmp(ss, "IN")==0){
scanf("%d",&a);
dq.push_front(a);
}
else{
if(dq.empty()){
cout<<"None"<<endl;
}
else{
cout<<dq.front()<<endl;
dq.pop_front();
}
}
}
}
}
return 0;
}
B - Parentheses Balance
* 判断括号的匹配问题
* stack
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <deque>
#include <stack>
using namespace std;
#define ll long long
#define ld long double
#define mm(a,b) memset(a,b,sizeof(a))
typedef pair<int,int> P;
int main()
{
int n;
cin>>n;
getchar();
while(n--)
{
string s;
const char *s1;
int len;
stack<char>x;
stack<char>y;
getline(cin,s);
if(s.size()==0)
{
cout<<"Yes"<<endl;
continue;
}
while(!x.empty())
{
x.pop();
}
while(!y.empty())
{
y.pop();
}
s1=s.c_str();
char *s2=new char[strlen(s1)+1];
strcpy(s2,s1);
len=strlen(s2);
for(int i=0;i<strlen(s2);i++)
{
x.push(s2[i]);
}
while(!x.empty())
{
while((!x.empty()&&!y.empty())&&((x.top()=='('&&y.top()==')')||(x.top()=='['&&y.top()==']')))
{
x.pop();y.pop();
}
if(!x.empty())
{
y.push(x.top());
x.pop();
}
}
if(x.empty()&&y.empty()) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
C - 简单计算器
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <deque>
#include <stack>
using namespace std;
#define ll long long
#define ld long double
#define mm(a,b) memset(a,b,sizeof(a))
typedef pair<int,int> P;
char str[205];
int cmp(int a,int b)
{
if(a==1||a==2) a=1;
else a=2;
if(b==1||b==2) b=1;
else b=2;
if(a>=b)
return 1;
return 0;
}
int fact(char c)
{
if(c=='+') return 1;
if(c=='-') return 2;
if(c=='*') return 3;
if(c=='/') return 4;
return -1;
}
int main()
{
while(gets(str))
{
if(!strcmp(str,"0")) return 0;
stack<double>ans;
stack<int>op;
int i,len=(int)strlen(str);
double s;
for(i=0;i<len;i++)
{
if(str[i]==' ')
continue;
if(str[i]>='0'&&str[i]<='9')
{
s=0;
while(str[i]>='0'&&str[i]<='9')
{
s=s*10+str[i]-'0';
i++;
}
ans.push(s);
}
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
{
int temp;
temp=fact(str[i]);
if(op.empty())
{
op.push(temp);
continue;
}
else
{
int t=op.top();
while(cmp(t,temp))
{
double a1=ans.top(); ans.pop();
double b1=ans.top(); ans.pop();
int c=op.top(); op.pop();
if(c == 1)
ans.push(a1+b1);
else if(c == 2)
ans.push(b1-a1);
else if(c == 3)
ans.push(a1*b1);
else if(c == 4)
ans.push(b1/a1);
if(op.empty())
break;
else
t=op.top();
}
op.push(temp);
}
}
}
while(!op.empty())
{
double a1=ans.top(); ans.pop();
double b1=ans.top(); ans.pop();
int c=op.top(); op.pop();
if(c == 1)
ans.push(a1+b1);
else if(c == 2)
ans.push(b1-a1);
else if(c == 3)
ans.push(a1*b1);
else if(c == 4)
ans.push(b1/a1);
}
printf("%.2lf\n",ans.top());
}
return 0;
}
D - Where is the Marble?
* 查找元素在排好序的数组中的位置
* sort
* lower_bound
* 我用的一个桶
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <deque>
#include <stack>
using namespace std;
#define ll long long
#define ld long double
#define mm(a,b) memset(a,b,sizeof(a))
typedef pair<int,int> P;
int main()
{
int m,n,num = 0;
while((cin >> m >> n) && (m!=0 && n!=0))
{
int count[10005] = {0};
for(int i = 0 ; i < m ; i++)
{
int temp;
cin >> temp;
count[temp]++;
}
cout << "CASE# " << ++num << ":" << endl;
for(int i = 0 ; i < n ; i++)
{
int a;
cin >> a;
int ans = 0;
if(count[a] == 0)
cout << a << " not found" << endl;
else
{
for(int j = 0 ; j !=a ; j++)
ans += count[j];
ans++;
cout << a << " found at " << ans << endl;
}
}
}
return 0;
}
E - The Blocks Problem
* 用vector模拟放木块
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <deque>
#include <stack>
using namespace std;
#define ll long long
#define ld long double
#define mm(a, b) memset(a,b,sizeof(a))
typedef pair<int, int> P;
int n;
vector<int> pile[30];
void my_find(int a, int &p, int &h) {
for (p = 0; p < n; p++)
for (h = 0; h < pile[p].size(); h++)
if (pile[p][h] == a) return;
}
void my_clear(int p, int h) {
for (int i = h + 1; i < pile[p].size(); i++) {
int t = pile[p][i];
pile[t].push_back(t);
}
pile[p].resize(h + 1);
}
void my_pile(int p, int h, int p2) {
for (int i = h; i < pile[p].size(); i++) pile[p2].push_back(pile[p][i]);
pile[p].resize(h);
}
void my_print() {
for (int i = 0; i < n; i++) {
printf("%d:", i);
for (int j = 0; j < pile[i].size(); j++) {
printf(" %d", pile[i][j]);
}
printf("\n");
}
}
int main() {
int a, b;
string s1, s2;
scanf("%d", &n);
for (int i = 0; i < n; i++) pile[i].push_back(i);
while (cin >> s1) {
if (s1 == "quit") break;
cin >> a >> s2 >> b;
int pa, pb, ha, hb;
my_find(a, pa, ha);
my_find(b, pb, hb);
if (pa == pb) continue;
if (s2 == "onto") my_clear(pb, hb);
if (s1 == "move") my_clear(pa, ha);
my_pile(pa, ha, pb);
}
my_print();
return 0;
}
F - Andy’s First Dictionary
* 用set模拟字典
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <deque>
#include <stack>
using namespace std;
#define ll long long
#define ld long double
#define mm(a,b) memset(a,b,sizeof(a))
typedef pair<int,int> P;
int main()
{
set<string>s;
set<string>::iterator it;
string str;
char c;
while ((c = getchar())!=EOF)
{
if (!isalpha(c))
{
if (str != "")
s.insert(str);
str = "";
}
else{
c = tolower(c);
str = str + c;
}
}
for (it = s.begin() ; it != s.end() ; ++it)
cout<<*it<<endl;
return 0;
}
G - 水果
* map
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <deque>
#include <stack>
using namespace std;
#define ll long long
#define ld long double
#define mm(a,b) memset(a,b,sizeof(a))
typedef pair<int,int> P;
#include<iostream>
#include<string>
#include<map>
using namespace std;
struct LIST{
map<string,int> LS;
};
int main()
{
int m;
cin>>m;
while(m--){
string name,place;
int number;
int n;
map<string,LIST>note;
map<string,LIST>::iterator iter;
map<string,int>temp;
cin>>n;
while(n--){
cin>>name>>place>>number;
note[place].LS[name] += number;
}
for(iter=note.begin();iter!=note.end();iter++)
{
cout<<iter->first<<endl;
map<string,int>::iterator iter2;
for(iter2 = iter->second.LS.begin(); iter2 != iter->second.LS.end();iter2++)
cout<<" |----"<<iter2->first<<"("<<iter2->second<<")"<<endl;
}
if(m != 0){
cout<<endl;
}
}
return 0;
}