(数据结构整理)NJUPT1054
这一篇博客以一些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的更多相关文章
- 转 Python常见数据结构整理
http://www.cnblogs.com/jeffwongishandsome/archive/2012/08/05/2623660.html Python常见数据结构整理 Python中常见的数 ...
- Java数据结构整理(一)
ava数据结构内容整理关键字: 数据结构 Collection:List.SetMap:HashMap.HashTable如何在它们之间选择一.Array , ArraysJava所有“存储及随机访问 ...
- redis数据结构整理(二)
摘要: 1.各个数据结构的应用举例 1.1 String类型应用举例 1.2List类型应用举例 1.3Set类型应用举例 1.4Sorted Set类型应用举例 1.5Hash类型应用举例 内容: ...
- redis数据结构整理(一)
摘要: 0.redis安装 1.redis的常用数据结构类型 1.1 String 1.2 List 1.3 Set 1.4 Sorted Set 1.5 Hash 2.redis是单进程单 ...
- C++ 基本数据结构整理
Hash Map (Unordered_map) Insert #include <unordered_map> using namespace std; unordered_map &l ...
- java数据结构整理(二)
一.List接口,有序的Collection接口,能够精确地控制每个元素插入的位置,允许有相同的元素 1.链表,LinkedList实现了List接口,允许null元素,提供了get().remove ...
- Python常见数据结构整理
Python中常见的数据结构可以统称为容器(container).序列(如列表和元组).映射(如字典)以及集合(set)是三类主要的容器. 一.序列(列表.元组和字符串) 序列中的每个元素都有自己的编 ...
- java常见数据结构整理
java中容器类数据结构主要在java.util包中. java.util包中三个重要的接口及特点:List(列表).Set(保证集合中元素唯一).Map(维护多个key-value键值对,保证key ...
- acm数据结构整理
一.区间划分 //区间划分+持久化并查集:区间连通情况统计. inline bool comp(Ask x, Ask y){return x.km == y.km ? x.l > y.l : x ...
随机推荐
- div,span等标签支持focus/blur事件
<div tabindex="0" hidefocus="true" onfocus='alert("得到焦点");' onblur= ...
- linux环境下删除包含特殊字符的文件或目录
linux环境下删除包含特殊字符的文件或目录 ls -liUse find command as follows to delete the file if the file has inode nu ...
- 【hdu 6321】Dynamic Graph Matching
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] DP 设f[i][j]表示前i个操作,已经匹配了的点的状态集合为j的方案数 对于+操作 有两种情况. 1.这条边作为匹配的边 2.这 ...
- 【【henuacm2016级暑期训练】动态规划专题 I】Gargari and Permutations
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 注意这k个序列每个都是排列. 如果在每个序列中都满足y出现在x之后的话. 那么我们从x连一条有向边至y (有一个序列不满足就不连 ( ...
- javascript-datatable错误提示
datatables插件在使用的时候出现了如下错误提示**出现此错误的原因可能是你写的table中没有加上<thead>和<tbody>标签所致** 来自为知笔记(Wi ...
- mybatis批量插入oracle大量数据记录性能问题解决
环境: mybatis + oracle11g r2 1.使用"直接路径插入"(以下sql语句中的"/*+append_values */"),而且使用key ...
- [Angular] Send Data via HTTP using Angular HttpParams
Obviously in a real world application we do not only fetch data from the backend, but we also send d ...
- cocos2dx-3.0创建Android项目时遇到的错误。
cocos run -p android出现 文件名称.文件夹名或卷标语法不对 Updated project.properties Updated local.properties Updated ...
- JavaSE入门学习24:Java面向对象补充
一Java中的Object类 Object类是全部Java类的父类.假设一个类没有使用extendskeyword明白标识继承另外一个类,那么这个类默认 继承Object类. public class ...
- vue组件样式添加scoped属性之后,无法被父组件修改。或者无法在本组件修改element UI样式
在vue开发中,需要使用scoped属性避免样式的全局干扰,但是这样在父组件中是无法被修改的,不仅如此如果项目中用了UI框架比如element Ui,这个时候在本组件也无法修改样式,因为权重问题.但是 ...