当前位置: 首页 > news >正文

NYOJ 简单数据结构

NYOJ 2 括号配对问题

栈的简单应用。可使用STL。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <stack>
 5 using namespace std;
 6 const int maxn=10000+5;
 7 
 8 char ch[maxn];
 9 stack<char> s;
10 
11 bool deal()
12 {
13     while(!s.empty())
14         s.pop();
15     int len=strlen(ch);
16     for(int i=0;i<len;i++)
17     {
18         if(ch[i]=='('||ch[i]=='[')
19             s.push(ch[i]);
20         else if(!s.empty()&&ch[i]==')')
21         {
22             if(s.top()=='(')
23                 s.pop();
24             else
25                 return 0;
26         }
27         else if(!s.empty()&&ch[i]==']')
28         {
29             if(s.top()=='[')
30                 s.pop();
31             else
32                 return 0;
33         }
34         else
35             return 0;
36     }
37     return s.empty();
38 }
39 
40 int main()
41 {
42     int t;
43     scanf("%d",&t);
44     while(t--)
45     {
46         scanf("%s",ch);
47         if(deal())
48             printf("Yes\n");
49         else
50             printf("No\n");
51     }
52     return 0;
53 }
View Code

 

NYOJ 5 Binary String Matching

简单模拟

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int maxn=1000+5;
 6 
 7 char a[maxn],b[maxn];
 8 
 9 int deal()
10 {
11     int num=0,j,len1,len2;
12     len1=strlen(a);
13     len2=strlen(b);
14     for(int i=0;i<len2;i++)
15     {
16         j=0;
17         while(a[i]==a[j])
18         {
19             i++;
20             j++;
21         }
22         if(j==len1)
23         {
24             num++;
25             j=1;
26         }    
27         i-=j;
28     }
29     return num;
30 }
31 
32 int main()
33 {
34     int t;
35     scanf("%d",&t);
36     while(t--)
37     {
38         scanf("%s%s",&a,&b);
39         printf("%d\n", deal());  
40     }
41     return 0;
42 }
View Code

 

 

 NYOJ 63 小猴子下落

有规律

观察可知,每一层小猴子经过节点往下都是左右轮流。

并且,猴子I为奇数时在二叉树左半部分,偶数时在右半部分,到子结点的猴子数目逐层减半。

所以,我们只需根据I的大小,判断每一层猴子会去左右哪一边。

这里用到一个性质:左子结点编号=父节点*2,右结点编号=父节点*2+1.

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int d,n,ans;
 9     while(scanf("%d%d",&d,&n)&&(d+n))
10     {
11         ans=1;
12         while(--d)
13         {
14             if(n%2==0)
15             {
16                 n/=2;
17                 ans=ans*2+1;
18             }
19             else
20             {
21                 n=(n+1)/2;
22                 ans=ans*2;
23             }
24         }
25         printf("%d\n",ans);
26     }
27     return 0;
28 }
View Code

 

NYOJ 93 汉诺塔(三)

简单模拟

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int maxn=70;
 6 
 7 struct node
 8 {
 9     int len;
10     int s[maxn];
11 }h[5];
12 
13 int n,p;
14 
15 void init()
16 {
17     h[1].len=h[2].len=h[3].len=-1;
18     for(int i=n;i>=1;i--)
19         h[1].s[++h[1].len]=i;
20 }
21 
22 int deal()
23 {
24     int x,y,xx,yy,f=0;
25     for(int i=0;i<p;i++)
26     {
27         scanf("%d%d",&x,&y);
28         if(f)
29             continue ;
30         if(h[x].len!=-1)
31         {
32             xx=h[x].len;yy=h[y].len;
33             if(yy!=-1&&h[y].s[yy]<h[x].s[xx])
34                 f=1;
35             else
36             {
37                 h[x].len--;
38                 h[y].len++;
39                 h[y].s[yy+1]=h[x].s[xx];
40             }
41         }
42         else
43             f=1;    
44     }
45     return !f;
46 }
47 
48 int main()
49 {
50     int t;
51     scanf("%d",&t);
52     while(t--)
53     {
54         scanf("%d%d",&n,&p);
55         init();
56         if(deal())
57             printf("legal\n");
58         else
59             printf("illegal\n");
60     }
61     return 0;
62 }
View Code

 

转载于:https://www.cnblogs.com/yang-/p/5471349.html

相关文章:

  • js对象的深浅拷贝
  • ★Kali信息收集~4.DNS系列
  • Promise面试题2实现异步串行执行
  • Flex的一些总结
  • SAP ERP和C4C Account和Contact的双向同步
  • Spring源码解析—— ClassPathResource类
  • Angular6错误 Service: No provider for Renderer2
  • 01串(dp)
  • 通用排序工具类
  • Python 进行 URL 跳转
  • 安卓使用Root权限实现后台模拟全局按键、触屏事件方法(类似按键精灵)
  • 第13期 DApp 榜单 :来,吃我这波安利
  • java swing启动时窗口最大化
  • 一行代码迁移TensorFlow 1.x到TensorFlow 2.0
  • Oracle 12c 数据库中scott用户不存在的解决方法
  • 《深入 React 技术栈》
  • Date型的使用
  • echarts的各种常用效果展示
  • HTTP 简介
  • JAVA SE 6 GC调优笔记
  • MobX
  • MySQL几个简单SQL的优化
  • 从重复到重用
  • 基于遗传算法的优化问题求解
  • 树莓派 - 使用须知
  • 数组大概知多少
  • 双管齐下,VMware的容器新战略
  • 算法-图和图算法
  • 算法之不定期更新(一)(2018-04-12)
  • 学习ES6 变量的解构赋值
  • 硬币翻转问题,区间操作
  • 源码之下无秘密 ── 做最好的 Netty 源码分析教程
  • 自定义函数
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • ​TypeScript都不会用,也敢说会前端?
  • $emit传递多个参数_PPC和MIPS指令集下二进制代码中函数参数个数的识别方法
  • (145)光线追踪距离场柔和阴影
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (C++)八皇后问题
  • (第二周)效能测试
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (二十三)Flask之高频面试点
  • (六)软件测试分工
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • .L0CK3D来袭:如何保护您的数据免受致命攻击
  • .NET中使用Redis (二)
  • @Autowired和@Resource装配
  • [ HTML + CSS + Javascript ] 复盘尝试制作 2048 小游戏时遇到的问题
  • [ MSF使用实例 ] 利用永恒之蓝(MS17-010)漏洞导致windows靶机蓝屏并获取靶机权限
  • [ 云计算 | AWS ] AI 编程助手新势力 Amazon CodeWhisperer:优势功能及实用技巧
  • [.NET 即时通信SignalR] 认识SignalR (一)