山东理工大学 2015计科、软件《数据结构与算法》实践能力专题训练4:串、数组、广义表

数据结构实验之串一: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" );
    }
}

#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" );
    }
}

代码菜鸟,如有错误,请多包涵!!!

如有帮助记得支持我一下谢谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值