这一篇博客以一些OJ上的题目为载体,整理一下数据结构。会陆续的更新。

。。

我们都知道,数据结构的灵活应用有时能让简化一些题目的解答。

一、栈的应用

1、NJUPT OJ 1054(回文串的推断)

回文串的推断:将一个字符串的一半存入一个栈中。然后从栈顶開始推断这个字符串是否是回文串

/*
* NJUPTOJ_1054.cpp
*
* Created on: 2014年5月22日
* Author: pc
*/ #include <iostream>
#include <cstdio> using namespace std; const int maxn = 300; void toLower(char arr[],int len){
int i;
for(i = 0 ; i < len ; ++i){
if(!islower(arr[i])){
arr[i] += 32;
}
}
} int main(){
char a[maxn];
char s[maxn]; while(gets(a)!=NULL){ int len = strlen(a);
toLower(a,len); int mid = len/2 - 1; int top = 0;
int i;
for(i = 0 ; i <= mid ; ++i){
s[++top] = a[i];
} int next;
if(len%2 == 0){
next = mid+1;
}else{
next = mid+2;
} for(i = next ; i <= len-1 ; ++i){
if(s[top] != a[i]){
break;
} --top;
} if(top != 0){
printf("Not Palindrome.\n");
}else{
printf("Bingle! Palindrome.\n");
}
} return 0; }

下面是再次做这道题时的代码:

/*
* njupt_1054_1.cpp
*
* Created on: 2014年9月6日
* Author: pc
*/ #include <iostream>
#include <cstdio> using namespace std; const int maxn = 300; /**
* 将一个字符串转化成小写
*/
void toLower(char a[]){
int len = strlen(a);
int i;
for(i = 0 ; i < len ; ++i){
if(islower(a[i]) == false){
a[i] += 32;
}
}
} void work(char a[]){ int len = strlen(a);
int s[len]; /**
*关于mid和next额理解:
*mid: 用来标记開始比較时栈顶的第一个元素
*next: 用来比較剩下的一半的字符串中開始比較时的第一个元素的位置
*/
int mid = len/2 - 1;
int next;
if(len%2 == 0){
next = mid+1;
}else{
next = mid+2;
} int top = 0;
int i;
for(i = 0 ; i <= mid ; ++i){
s[++top] = a[i];
} for(i = next ; i <= len-1 ; ++i){
if(s[top] != a[i]){
break;
} --top;
} if(top == 0){
printf("Bingle! Palindrome.\n");
}else{
printf("Not Palindrome.\n");
} } int main(){ char s[maxn]; /**
* 不知道行数时的输入的处理方式...
*/
while(gets(s) != NULL){
toLower(s);
work(s);
} return 0; }

2、NEFU OJ 194 回文字符串

算法思想和上面的是一样的

/*
* NEFU_194.cpp
*
* Created on: 2014年5月23日
* Author: pc
*/ #include <iostream>
#include <cstdio> using namespace std; string a;
string s; int main() {
int t;
scanf("%d", &t);
while (t--) { cin >> a;
int len = a.length(); int mid = len / 2 - 1; int top = 0;
int i; for (i = 0; i <= mid; ++i) {
top += 1;
s[top] = a[i];
} int next;
if (len % 2 == 0) {
next = mid + 1;
} else {
next = mid + 2;
} for (i = next; i <= len - 1; ++i) {
if (s[top] != a[i]) {
break;
} --top;
} if (top != 0) {
printf("NO\n");
} else {
printf("YES\n");
}
} return 0;
}

3、NYOJ 1002 括号的匹配

思想:假设输入的符号是(、[则直接进栈,假设是],则推断此事最后一个是否是[,假设是[出栈,否则]进栈

/*
* NY_2_1.cpp
*
* Created on: 2014年5月25日
* Author: pc
*/ #include <iostream>
#include <cstdio>
#include <stack> using namespace std; int main(){ string a;
int t;
scanf("%d",&t);
while(t--){
stack<char> s; cin >> a;
int len = a.length();
int i;
for(i = 0 ; i < len ; ++i){
if(a[i] == '(' || a[i] == '['){
s.push(a[i]);
}else if(a[i] == ')'){
if(!s.empty() && s.top() == '('){
s.pop();
}else{
s.push(a[i]);
}
}else if(a[i] == ']'){
if(!s.empty() && s.top() == '['){
s.pop();
}else{
s.push(a[i]);
}
}
} if(s.empty()){
printf("Yes\n");
}else{
printf("No\n");
}
} return 0;
}

(数据结构整理)NJUPT1054的更多相关文章

  1. 转 Python常见数据结构整理

    http://www.cnblogs.com/jeffwongishandsome/archive/2012/08/05/2623660.html Python常见数据结构整理 Python中常见的数 ...

  2. Java数据结构整理(一)

    ava数据结构内容整理关键字: 数据结构 Collection:List.SetMap:HashMap.HashTable如何在它们之间选择一.Array , ArraysJava所有“存储及随机访问 ...

  3. redis数据结构整理(二)

    摘要: 1.各个数据结构的应用举例 1.1 String类型应用举例 1.2List类型应用举例 1.3Set类型应用举例 1.4Sorted Set类型应用举例 1.5Hash类型应用举例 内容: ...

  4. redis数据结构整理(一)

    摘要: 0.redis安装 1.redis的常用数据结构类型 1.1  String 1.2  List 1.3  Set 1.4  Sorted Set 1.5  Hash 2.redis是单进程单 ...

  5. C++ 基本数据结构整理

    Hash Map (Unordered_map) Insert #include <unordered_map> using namespace std; unordered_map &l ...

  6. java数据结构整理(二)

    一.List接口,有序的Collection接口,能够精确地控制每个元素插入的位置,允许有相同的元素 1.链表,LinkedList实现了List接口,允许null元素,提供了get().remove ...

  7. Python常见数据结构整理

    Python中常见的数据结构可以统称为容器(container).序列(如列表和元组).映射(如字典)以及集合(set)是三类主要的容器. 一.序列(列表.元组和字符串) 序列中的每个元素都有自己的编 ...

  8. java常见数据结构整理

    java中容器类数据结构主要在java.util包中. java.util包中三个重要的接口及特点:List(列表).Set(保证集合中元素唯一).Map(维护多个key-value键值对,保证key ...

  9. acm数据结构整理

    一.区间划分 //区间划分+持久化并查集:区间连通情况统计. inline bool comp(Ask x, Ask y){return x.km == y.km ? x.l > y.l : x ...

随机推荐

  1. CORS与JSONP的区别

    CORS与JSONP: a. JSONP只能实现GET请求,而CORS支持所有类型的HTTP请求. b. 使用CORS,开发者可以使用普通的XMLHttpRequest发起请求和获得数据,比起JSON ...

  2. Eclipse配置Maven私服

    Eclipse配置Maven私服 前言: 搭建Maven私有仓库的主要目的,是为了在团队多人开发时,只要内网的私有仓库有下载过依赖的jar包,就直接从私有仓库获取,不再通过外网的中央仓库.如果私服上面 ...

  3. AssetBundle打包优化解决方式

    第一阶段:AssetBundle出一套解决方式 1.解决如今同一个资源打2个bundle的冗余问题 2.測试验证节省资源的比率是多少 问题拆分 一.bundle反复 问  题  :同样资源拆分问题? ...

  4. 各种List、Map、Set的比較

    前言:Java中用不同的数据结构(哈希表/动态数组/平衡树/链表)实现了不同的集合接口(List/Map/Set).大多数情况下,集合类被用于不须要线程安全地环境,所以后来的集合实现都没有再採用同步以 ...

  5. EF的CRUD

    已经知道EF就是一个能够使得编程人员用面向对象的思想操作数据库的框架,那么在最初学习SQL的时候我们就知道对数据库的操作就是增删改查.万变不离其宗. EF也是操作数据库的当然也就是要对数据库实现增删改 ...

  6. java日期类型与字符串类型的相互转换

    package cn.zwq.convert; import java.text.ParseException; import java.text.SimpleDateFormat; import j ...

  7. (转载) Android RecyclerView 使用完全解析 体验艺术般的控件

    Android RecyclerView 使用完全解析 体验艺术般的控件 标签: Recyclerviewpager瀑布流 2015-04-16 09:07 721474人阅读 评论(458) 收藏  ...

  8. 51nod 1572 宝岛地图 (预处理四个方向的最大步数优化时间,时间复杂度O(n*m+k))

    题目: 这题如果没有时间限制的话暴力可以解,暴力的话时间复杂度大概是O(k*n),1s的话非常悬. 所以我们需要换个思路,我们对每个点预处理四个方向最多能走的步数,这个预处理时间复杂度是O(n*m). ...

  9. BootStrap学习(三)——重写首页之导航栏和轮播图

    1.按钮 1)帮助文档:http://v3.bootcss.com/css/#buttons 2).btn-lg..btn-sm..btn-xs可以设置按钮的不同尺寸 3).active类设置按钮的激 ...

  10. 转Hibernate继承

    hibernate继承映射 以下测试是在mysql中进行的. 1.单表方式 Animal.java @Entity @Inheritance(strategy=InheritanceType.SING ...