在第四章的学习中,主要学习了串和数组,相对于上学期学习过的数组,串的内容相对比较难,在关于串的应用方面也遇到了一些问题。
给定一个主串S(长度<=10^6)和一个模式T(长度<=10^5),要求在主串S中找出与模式T相匹配的子串,返回相匹配的子串中的第一个字符在主串S中出现的位置。
先上题目
输入格式:
输入有两行: 第一行是主串S; 第二行是模式T.
输出格式:
输出相匹配的子串中的第一个字符在主串S中出现的位置。若匹配失败,输出0.
输入样例:
在这里给出一组输入。例如:
aaaaaba
ba
输出样例:
在这里给出相应的输出。例如:
6
在做这道题的时候开始一直不懂next数组(虽然现在也不是很懂QAQ),尝试着按照书本上的方式去做,也在网站上查阅了一些资料,最后做出这道题如下:
#include <iostream>
#include <string.h>
using namespace std;
#include <string.h>
using namespace std;
void get_next(string s,int next[])
{
int length = s.length(); //求出字串的长度
int i = 0,j = -1;
next[0] = -1;
while(i<length)
{
if (j==-1 || s[i]==s[j])next[++i] = ++j;
else j = next[j];
}
//根据字串求出next数组
}
{
int length = s.length(); //求出字串的长度
int i = 0,j = -1;
next[0] = -1;
while(i<length)
{
if (j==-1 || s[i]==s[j])next[++i] = ++j;
else j = next[j];
}
//根据字串求出next数组
}
int get_position(string a,string b)
{
int i=0 , j=0; //主串和字串都从第一个字符开始比较
int next[ b.length() ]; //为next数组创建空间
get_next(b,next); //得到next数组
while( i<a.length() && j<b.length() ) //两个串均未比较到末尾
{
if(j==-1 || a[i]==b[j] ) //继续比较后续字符
{
i++;
j++;
}
else j=next[j]; //模式串向右移动
}
if( j==b.length() )
return i-j+1 ; //匹配成功,返回位置
else
return 0; //匹配失败
}
{
int i=0 , j=0; //主串和字串都从第一个字符开始比较
int next[ b.length() ]; //为next数组创建空间
get_next(b,next); //得到next数组
while( i<a.length() && j<b.length() ) //两个串均未比较到末尾
{
if(j==-1 || a[i]==b[j] ) //继续比较后续字符
{
i++;
j++;
}
else j=next[j]; //模式串向右移动
}
if( j==b.length() )
return i-j+1 ; //匹配成功,返回位置
else
return 0; //匹配失败
}
int main()
{
string a,b; //分别定义主串和字串
{
string a,b; //分别定义主串和字串
cin >> a >> b; //分别输入主串和字串
cout<<get_position(a,b); //得到匹配的位置
return 0;
}
cout<<get_position(a,b); //得到匹配的位置
return 0;
}
但是匹配一直失败,自己也找不出问题,最后试着每个代码都改一改才发现是while( i<a.length() && j<b.length()这方面出问题,最后上百度查阅了下发现之前有类似问题存在,网址:https://blog.csdn.net/lucasdove/article/details/50904725
解决方法:在前面先设定两个int型参数 alen 和 blen,分别等于a.length() 和 b.length(),原因是string函数里的length函数返回的是无符号数,在作为判断条件时会出现问题。
附上正确代码:
#include <iostream>
#include <string.h>
using namespace std;
#include <string.h>
using namespace std;
void get_next(string s,int next[])
{
int length = s.length(); //求出字串的长度
int i = 0,j = -1;
next[0] = -1;
while(i<length)
{
if (j==-1 || s[i]==s[j])
{
next[++i] = ++j;
}
{
int length = s.length(); //求出字串的长度
int i = 0,j = -1;
next[0] = -1;
while(i<length)
{
if (j==-1 || s[i]==s[j])
{
next[++i] = ++j;
}
else
{
j = next[j];
}
}
//根据字串求出next数组
}
{
j = next[j];
}
}
//根据字串求出next数组
}
int get_position(string a,string b)
{
int i=0 , j=0; //主串和字串都从第一个字符开始比较
int alen=a.length() ;//求出a串长度
int blen=b.length() ;//求出b串长度
int next[blen]; //为next数组创建空间
get_next(b,next); //得到next数组
while( i<alen && j<blen ) //两个串均未比较到末尾
{
if(j==-1 || a[i]==b[j] ) //继续比较后续字符
{
i++;
j++;
}
else j=next[j]; //模式串向右移动
}
if( j==blen )
return i-j+1 ; //匹配成功,返回位置
else
return 0; //匹配失败
}
{
int i=0 , j=0; //主串和字串都从第一个字符开始比较
int alen=a.length() ;//求出a串长度
int blen=b.length() ;//求出b串长度
int next[blen]; //为next数组创建空间
get_next(b,next); //得到next数组
while( i<alen && j<blen ) //两个串均未比较到末尾
{
if(j==-1 || a[i]==b[j] ) //继续比较后续字符
{
i++;
j++;
}
else j=next[j]; //模式串向右移动
}
if( j==blen )
return i-j+1 ; //匹配成功,返回位置
else
return 0; //匹配失败
}
int main()
{
string a,b; //分别定义主串和字串
{
string a,b; //分别定义主串和字串
cin >> a >> b; //分别输入主串和字串
cout<<get_position(a,b); //得到匹配的位置
return 0;
}
cout<<get_position(a,b); //得到匹配的位置
return 0;
}