一个一个写着太麻烦了,每天有限量,整合到一起写了。
16.括号匹配
代码如下:
#include <stdio.h>
#include <string.h>
long long L[100010]={0},R[100010]={0},sum=0,record;
int match(char c1,char c2)
{
return c1=='('&&c2==')'?1:0;
}
void simplify(char s[])
{
char stack[100010];
int count,top=-1;
for(int i=0;i<strlen(s);i++)
{
if(top!=-1&&match(stack[top],s[i])) top--;
else stack[++top]=s[i];
}
if (top==-1)
{
sum++;
return;
}
char c=stack[top];
count=top+1;
for(;top>=0;top--)
{
if(stack[top]!=c) return;
}
if (c=='(') L[count]++;
else R[count]++;
}
int main()
{
int n,i;
char str[100010];
scanf("%d\n",&n);
for(i=0;i<n;i++)
{
scanf("%s",str);
simplify(str);
}
record=sum/2;
for(i=0; i<100010; i++) record+=L[i]<R[i]?L[i]:R[i];
printf("%lld\n",record);
}
17. 看楼房
Description
小张在暑假时间进行了暑期社会调查。调查的内容是楼房的颜色如何影响人们的心情。于是他找到了一个楼房从左到右排成一排的小区,这个小区一共有栋楼房,每个楼房有一个颜色和一个高度。小张调查的内容为每次他站在第栋楼和第栋楼之间向左看,他记录下此时他看到的楼房颜色数作为他的调查结果。
由于小张在暑假时间沉迷游戏来不及做实地调查,只好拜托你将调查结果告诉他。
Input
本题有多组数据。
Output
每组数据输出个数,第个数表示他站在第栋楼和第栋楼之间向左看,能够看到的楼房颜色数。
Notes
在从左向右看楼房的时候,左边较矮的楼房会被右边较高的楼房挡住。
测试输入 | 期待的输出 | 时间限制 | 内存限制 | 额外进程 | |
---|---|---|---|---|---|
测试用例 1 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
测试用例 5 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define WIDTH 1000010
#define HASH_SIZE 1000003
int T,i,n,temp,color_class,top_color=-1,top_height=-1;
int color[WIDTH],height[WIDTH],stack_color[WIDTH],stack_height[WIDTH];
typedef struct HashNode
{
int key;
int value;
bool used;
}HashNode;
HashNode hashTable[HASH_SIZE];
unsigned int hash(int key)
{
unsigned int h=0;
h=key%HASH_SIZE;
return h;
}
void insert(int key,int value)
{
unsigned int pos=hash(key);
while (hashTable[pos].used&&hashTable[pos].key!=key) pos=(pos+1)%HASH_SIZE;
hashTable[pos].key=key;
hashTable[pos].value=value;
hashTable[pos].used=true;
}
int search(int key)
{
unsigned int pos=hash(key);
while (hashTable[pos].used&&hashTable[pos].key!=key) pos=(pos+1)%HASH_SIZE;
if (hashTable[pos].used) return hashTable[pos].value;
else return 0;
}
int main()
{
scanf("%d",&T);
while (T-->0)
{
while(top_color!=-1)
{
top_color--;
top_height--;
}
memset(hashTable,0,sizeof(hashTable));
color_class=0;
scanf("%d", &n);
for(i=0;i<n;i++) scanf("%d",&color[i]);
for(i=0;i<n;i++) scanf("%d",&height[i]);
for(i=0;i<n;i++)
{
if(top_height==-1||height[i]<stack_height[top_height])
{
stack_color[++top_color]=color[i];
stack_height[++top_height]=height[i];
int count=search(color[i])+1;
insert(color[i],count);
if(count==1) color_class++;
}
else
{
while(top_height!=-1&&height[i]>=stack_height[top_height])
{
int count=search(stack_color[top_color])-1;
insert(stack_color[top_color],count);
if(count==0) color_class--;
top_height--;
top_color--;
}
if (top_height==-1||height[i]<stack_height[top_height])
{
stack_color[++top_color]=color[i];
stack_height[++top_height]=height[i];
int count=search(color[i])+1;
insert(color[i],count);
if(count==1) color_class++;
}
}
printf("%d%c",color_class,i==n-1?'\n':' ');
}
}
return 0;
}
18. 填坑Ⅰ
代码如下:
#include <stdio.h>
#include <stdbool.h>
#define WIDTH 200001
int height[WIDTH];
int face[WIDTH];
int top=-1;
void push(int x)
{
face[++top]=x;
}
void pop()
{
--top;
}
int peek()
{
return face[top];
}
int main(void)
{
int n,i,max;
while(scanf("%d",&n)!=EOF)
{
top=-1;
max=0;
for(i=0;i<n;i++)
{
scanf("%d",&height[i]);
if(height[i]>max) max=height[i];
}
for(i=0;i<n;i++)
height[i]=(max-height[i])%2;
for(i=0;i<n;i++)
{
(top==-1)?push(height[i]):((height[i]==peek())?pop():push(height[i]));
}
(top<=0)?printf("YES\n"):printf("NO\n");
}
}
19. 填坑Ⅱ
代码如下:
#include <stdio.h>
#include <string.h>
int main()
{
long long n,i,max,cnt,top=-1;
long long stk[200010],a[200010]={0};
while(scanf("%lld",&n)!=EOF)
{
memset(a,0,sizeof(a[0]));
top = -1;
max = 0;
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
if (a[i]>max) max=a[i];
}
for(i=0;i<n;i++)
{
if(!(top==-1)&&a[i]==stk[top]) top--;
else
{
if(!(top==-1)&stk[top]<a[i]) break;
else stk[++top]=a[i];
}
}
cnt=top+1;
if(!cnt) printf("YES\n");
else if(cnt==1&&stk[top]==max) printf("YES\n");
else printf("NO\n");
}
}
20. 选举
在遥远的相簿国有两个种族,分别是冬马族和雪菜族,每个人都属于这两个种族之一。如今,又到了选举元首的时候了,所有 n 个人排成一排等待投票。
相簿国的选举制度比较奇特,并不是看谁的票数多,而是使用一票否决制度,并且所有人都可以投一张票或者不投票。如果排在前面的人把排在后面的人一票否决了,那么后面的人不能参与竞选,也不能投出他的那一票了。而如果排在后面的人把排在前面的人一票否决了,那么排在前面的人不能参与竞选,但是已经投出的那一票仍然有效。如果一轮投完仍然有不只一个竞选者,那么这些留下的人再投一轮,还没有停止就再投一轮……直到剩下一人为止,但投票的先后顺序不变。
为了保证每轮投票中都有更多人可以投票,规定竞选者优先投票给在他投票时已经投过票的异族人,每轮投票结束时重置竞选人的投票状态。
所有人都希望自己族的人能当选元首(而不是希望自己当元首),并且会为此采取最优策略。现在给出排队中每个人的种族,请问最后是哪个种族的人会当选元首?
输入
输入第一行一个数 n ,表示相簿国里的人数。
第二行一个长度为 n 的字符串 s ,从前到后依次表示排队投票的人所属的种族。D 表示冬马族,而 X 表示雪菜族。
输出
输出一个字符 D 或 X ,表示是哪一族的人当选。
注意
数据保证1<=n <=200000。
测试用例
用例1.
输入:
5↵
DDXXX↵
输出:
D↵
用例2.
输入:
6↵
DDXXXX↵
输出:
X↵
测试输入 | 期待的输出 | 时间限制 | 内存限制 | 额外进程 | |
---|---|---|---|---|---|
测试用例 1 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
测试用例 2 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
代码如下:
#include <stdio.h>
#define max 200010
int main()
{
char S[max],s[max],head;
int n,i,num,D=0,X=0,D1=0,X1=0,top=-1;
scanf("%d",&n);
scanf("%s",s);
for(i=0;i<n;i++)
{
if(s[i]=='D')
{
if(X==0)
{
if(top==-1)
{
S[++top]='D';
D++;
}
else
{
if(S[top]=='D')
{
S[++top]='D';
D++;
}
else
{
top--;
num=1;
while(i<n-1&&s[i+1]=='D'&&top!=-1&&S[top]=='X')
{
i++;
top--;
num++;
}
while(num--) S[++top]='D';
}
}
}
else X--;
}
else if(s[i]=='X')
{
if(D==0)
{
if(top==-1)
{
S[++top]='X';
X++;
}
else
{
if(S[top]=='X')
{
S[++top]='X';
X++;
}
else
{
top--;
num=1;
while(i<n-1&&s[i+1]=='X'&&top!=-1&&S[top]=='D')
{
i++;
top--;
num++;
}
while(num--) S[++top]='X';
}
}
}
else D--;
}
}
while(top!=-1)
{
if(top==0) head=S[top];
if(S[top]=='D') D1++;
else X1++;
top--;
}
if(D1>X1) printf("D\n");
else if(X1>D1) printf("X\n");
else printf("%c\n",head);
}