山东理工大学 2015计科、软件《数据结构与算法》实践能力专题训练4:串、数组、广义表
2016-10-22 21:38
253 查看
数据结构实验之串一:KMP简单应用
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#define MAX_N 1000005
using namespace std;
int next[MAX_N];
void nextSum( char *s )
{
int i = 0, j = -1;
next[0] = -1;
int len = strlen(s);
while ( i < len )
{
if ( j == -1 || *(s+i) == *(s+j) )
{
i++, j++;
next[i] = j;
}
else
j = next[j];
}
}
int kmp( char s[], char c[] )
{
int i = 0, j = 0;
int len_str = strlen(s);
int len_ch = strlen(c);
while ( i < len_ch && j < len_str )
{
if ( j == -1 || c[i] == s[j] )
i++, j++;
else
j = next[j];
}
if ( j >= len_str )
return i-len_str+1;
return -1;
}
int main()
{
char ch[MAX_N];
char str[MAX_N];
while ( ~scanf ( "%s", ch ) )
{
scanf ( "%s", str );
nextSum( str );
int sum = kmp( str, ch );
printf ( "%d\n", sum );
}
}
数据结构实验之串二:字符串匹配
代码简单点的:因为题目给的字符串很小,我们可以直接暴力即可以了
#include<cstring>
#include <cstdio>
#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
string s1, s2;
while ( cin >> s1 )
{
cin >> s2;
int ok = 1;
if ( s1.find(s2) == string::npos ) //s1里是否有子串s2,否就执行
ok = 0;
printf ( ok ? "YES\n" : "NO\n" );
}
}
数据结构实验之串三:KMP应用
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int a[10000002];
int b[100005];
int next[100005];
int n, m;
void nextSum()
{
int i = 0, j = -1;
next[0] = -1;
while ( i < m )
{
if ( j == -1|| b[i] == b[j] )
{
i++, j++;
next[i] = j;
}
else
j = next[j];
}
}
int kmp(int x)
{
int i = x, j = 0;
while ( i < n && j < m )
{
if ( j == -1 || a[i] == b[j] )
i++, j++;
else
j = next[j];
}
if ( j >= m )
return i-m+1;
return -1;
}
int main()
{
int i;
scanf ( "%d", &n );
for ( i = 0;i < n; i++ )
scanf ( "%d", &a[i] );
scanf ( "%d", &m );
for ( i = 0;i < m; i++ )
scanf ( "%d", &b[i] );
nextSum();
int sum = kmp(0);
if ( sum != -1 )
{
int sum1 = kmp(sum);
if ( sum1 == -1 )
printf ( "%d %d\n", sum, sum+m-1);
else
printf ( "-1\n" );
}
else
printf ( "-1\n" );
}数据结构实验之数组一:矩阵转置
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int a[102][102];
int main()
{
int n, m, i, j;
while ( ~scanf ( "%d %d", &n, &m ) )
{
for ( i = 0;i < n; i++ )
{
for ( j = 0;j < m; j++ )
{
scanf ( "%d", &a[i][j] );
}
}
for ( i = 0;i < m; i++ )
{
int ok = 1;
for ( j = 0;j < n; j++ )
{
if ( ok == 1 )
{
ok = 0;
printf ( "%d", a[j][i] );
}
else
printf ( " %d", a[j][i] );
}
printf ( "\n" );
}
printf ( "\n" );
}
}
数据结构实验之数组三:快速转置
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
int x, y, z;
}a[10005];
bool cmp(node q, node p)
{
if ( q.y != p.y )
return q.y < p.y;
return q.x < p.x;
}
int main()
{
int n, m, i, h;
while ( ~scanf ( "%d %d %d", &n, &m, &h ) )
{
for ( i = 0;i < h; i++ )
{
scanf ( "%d %d %d", &a[i].x, &a[i].y, &a[i].z );
}
sort(a, a+h, cmp);
for ( i = 0;i < h; i++ )
{
printf ( "%d %d %d\n", a[i].y, a[i].x, a[i].z );
}
}
}
数据结构实验之数组二:稀疏矩阵
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
int x, y, z;
}a[10005];
int main()
{
int n, m, i, h;
while ( ~scanf ( "%d %d %d", &n, &m, &h ) )
{
int ok = 0;
for ( i = 0;i < h; i++ )
{
scanf ( "%d %d %d", &a[i].x, &a[i].y, &a[i].z );
}
scanf ( "%d", &n );
for ( i = 0;i < h; i++ )
{
if ( n == a[i].z )
ok = 1;
}
printf ( ok ? "OK\n" : "ERROR\n" );
}
}
代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下谢谢!!!
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#define MAX_N 1000005
using namespace std;
int next[MAX_N];
void nextSum( char *s )
{
int i = 0, j = -1;
next[0] = -1;
int len = strlen(s);
while ( i < len )
{
if ( j == -1 || *(s+i) == *(s+j) )
{
i++, j++;
next[i] = j;
}
else
j = next[j];
}
}
int kmp( char s[], char c[] )
{
int i = 0, j = 0;
int len_str = strlen(s);
int len_ch = strlen(c);
while ( i < len_ch && j < len_str )
{
if ( j == -1 || c[i] == s[j] )
i++, j++;
else
j = next[j];
}
if ( j >= len_str )
return i-len_str+1;
return -1;
}
int main()
{
char ch[MAX_N];
char str[MAX_N];
while ( ~scanf ( "%s", ch ) )
{
scanf ( "%s", str );
nextSum( str );
int sum = kmp( str, ch );
printf ( "%d\n", sum );
}
}
数据结构实验之串二:字符串匹配
代码简单点的:因为题目给的字符串很小,我们可以直接暴力即可以了
#include<cstring>
#include <cstdio>
#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
string s1, s2;
while ( cin >> s1 )
{
cin >> s2;
int ok = 1;
if ( s1.find(s2) == string::npos ) //s1里是否有子串s2,否就执行
ok = 0;
printf ( ok ? "YES\n" : "NO\n" );
}
}
#include<cstring> #include <cstdio> #include <iostream> #include<algorithm> using namespace std; const int MAX_N = 1000005; int next[MAX_N]; void nextSum(string s) { int i = 0, j = -1; next[0] = -1; int len = s.length(); while ( i < len ) { if ( j== -1|| s[j] == s[i] ) { i++, j++; next[i] = j; } else j = next[j]; } } bool kmp(string s1, string s2) { int i = 0, j = 0; int len_s1 = s1.length(); int len_s2 = s2.length(); while ( i < len_s1 && j < len_s2 ) { if ( j == -1||s1[i] == s2[j] ) i++, j++; else j = next[j]; } if (j >= len_s2 ) return true; return false; } int main() { string s1, s2; while ( cin >> s1 ) { cin >> s2; nextSum(s2); bool ok = kmp(s1, s2); printf ( ok ? "YES\n" : "NO\n" ); } }
数据结构实验之串三:KMP应用
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int a[10000002];
int b[100005];
int next[100005];
int n, m;
void nextSum()
{
int i = 0, j = -1;
next[0] = -1;
while ( i < m )
{
if ( j == -1|| b[i] == b[j] )
{
i++, j++;
next[i] = j;
}
else
j = next[j];
}
}
int kmp(int x)
{
int i = x, j = 0;
while ( i < n && j < m )
{
if ( j == -1 || a[i] == b[j] )
i++, j++;
else
j = next[j];
}
if ( j >= m )
return i-m+1;
return -1;
}
int main()
{
int i;
scanf ( "%d", &n );
for ( i = 0;i < n; i++ )
scanf ( "%d", &a[i] );
scanf ( "%d", &m );
for ( i = 0;i < m; i++ )
scanf ( "%d", &b[i] );
nextSum();
int sum = kmp(0);
if ( sum != -1 )
{
int sum1 = kmp(sum);
if ( sum1 == -1 )
printf ( "%d %d\n", sum, sum+m-1);
else
printf ( "-1\n" );
}
else
printf ( "-1\n" );
}数据结构实验之数组一:矩阵转置
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int a[102][102];
int main()
{
int n, m, i, j;
while ( ~scanf ( "%d %d", &n, &m ) )
{
for ( i = 0;i < n; i++ )
{
for ( j = 0;j < m; j++ )
{
scanf ( "%d", &a[i][j] );
}
}
for ( i = 0;i < m; i++ )
{
int ok = 1;
for ( j = 0;j < n; j++ )
{
if ( ok == 1 )
{
ok = 0;
printf ( "%d", a[j][i] );
}
else
printf ( " %d", a[j][i] );
}
printf ( "\n" );
}
printf ( "\n" );
}
}
数据结构实验之数组三:快速转置
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
int x, y, z;
}a[10005];
bool cmp(node q, node p)
{
if ( q.y != p.y )
return q.y < p.y;
return q.x < p.x;
}
int main()
{
int n, m, i, h;
while ( ~scanf ( "%d %d %d", &n, &m, &h ) )
{
for ( i = 0;i < h; i++ )
{
scanf ( "%d %d %d", &a[i].x, &a[i].y, &a[i].z );
}
sort(a, a+h, cmp);
for ( i = 0;i < h; i++ )
{
printf ( "%d %d %d\n", a[i].y, a[i].x, a[i].z );
}
}
}
数据结构实验之数组二:稀疏矩阵
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
int x, y, z;
}a[10005];
int main()
{
int n, m, i, h;
while ( ~scanf ( "%d %d %d", &n, &m, &h ) )
{
int ok = 0;
for ( i = 0;i < h; i++ )
{
scanf ( "%d %d %d", &a[i].x, &a[i].y, &a[i].z );
}
scanf ( "%d", &n );
for ( i = 0;i < h; i++ )
{
if ( n == a[i].z )
ok = 1;
}
printf ( ok ? "OK\n" : "ERROR\n" );
}
}
代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下谢谢!!!
相关文章推荐
- 山东理工大学 2015级计科、软件《数据结构与算法》实践能力专题训练5:二叉树
- 2015级计科软件《数据结构与算法》实践能力专题训练7查找
- 2015级计科软件《数据结构与算法》实践能力专题训练6图论
- 2015级计科软件《数据结构与算法》实践能力专题训练8排序
- 面试问题:专业基础知识,软件工程知识,英语水平,实践能力
- 2014全国大学生软件实践能力大赛
- [软件架构训练基础教程-8]软件应用实践
- 软件工程的实践项目课程的自我目标(要求包含对实践项目完成后学习到的能力的预期,对项目课程的期望、对项目的愿景规划)
- 金旭亮老师课程《面向对象软件开发实践之基本技能训练》第二章作业
- 第九周 数据结构实践项目——数组和广义表【项目3.2-- 稀疏矩阵相加】
- 第九周 数据结构实践项目——数组和广义表【项目3.1-稀疏矩阵的三元组表示的实现及应用】
- 产品研发过程管理专题——软件测试过程管理实践
- 数据结构实践项目——数组和广义表
- 后缀数组专题训练
- 职业技能——面向对象软件开发实践之基本技能训练
- 大连软件园对日软件开发设计能力提升专题研讨会胜利举办
- 后缀数组专题训练
- 第四届CCF软件能力认证(CSP2015) 第五题(最小花费)题解
- 软件架构训练基础教程之面向对象技术
- [原创] 敏捷软件开发管理实践 (四) ——让每个人学会更好的沟通