这一篇博客以一些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. JS中的NaN

    什么是NaN?它的类型是什么?如何可靠地测试一个值是否等于NaN? NaN属性表示“不是数字”的值.这个特殊值是由于一个操作数是非数字的(例如“abc”/ 4)或者因为操作的结果是非数字而无法执行的. ...

  2. java字符文件的读写

    1.java文件读写,首先我们需要导入相应的包:java.io.*; 2.代码如下: package Demo1; import java.io.*; public class FileWirteTe ...

  3. Linux学习总结(15)——提高 Vim 和 Shell 效率的 9 个建议

    你上一次使用 CAPSLOCK 键是什么时候?很久没有了对不对?噢,我也是,它已经被遗忘了,它浪费了键盘上一个黄金位置.让我们把它重映射成 Control 键来发挥它的作用吧!这里告诉了你在不同的操作 ...

  4. ASP.NET-Microsoft.Management.Infrastructure错误

    错误如图所示,将MVC发布到IIS上就会出现这个错误,我用到了NPOI这个EXCEL插件,不知道是不是这个造成的,但是实在找不到解决方案,就直接将BIN目录下的这个Microsoft.Manageme ...

  5. 15 个经常使用的 SQL Server 高级语法

    1.case-end (详细的值) case后面有值,相当于c#中的switch case 注意:case后必须有条件,而且when后面必须是值不能为条件. -----------------case ...

  6. 初识BeeFramework

    由于近期的项目须要,Hybrid开发成为我開始学习的新知识.非常早之前就了解到两个开发框架--BeeFramework 和 Samurai,可是由于本人一直没有闲暇去研究,所以就一直搁置一旁了.近期才 ...

  7. 面试-MySQL

    1  事务的特性 事务具有四个特性:原子性(Atomicity).一致性(Consistency).隔离性(Isolation)和持续性(Durability).这四个特性也简称ACID性. (1)原 ...

  8. android drawable资源调用使用心得

    1. 调用顺序 android 调用应用图片资源时,会优先选择当前手机屏幕dpi对应的的文件夹(如drawable-ldpi, drawable-mdpi, drawable-hdpi, drawab ...

  9. DB-MySQL:目录

    ylbtech-DB-MySQL:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbt ...

  10. String转换成int型

    private boolean judge(String str){ int year = 0; try{ year = Integer.valueOf(str).intValue(); }catch ...