串的基本操作(KMP算法实现)
#include <iostream>
#include <math.h>
using namespace std;
void StrAssign(char *T)
{
char ch;
int i=1;
cout<<"Please enter a string.0 is exit."<<endl;
while (cin>>ch&&ch!='0') {
T[i++]=ch;
}
T[0]=i-1+'0';
cout<<"Setting successfully!"<<endl;
}
int StrCopy(char *T,char *S)
{
if (S[0]=='0') {
cout<<"String is empty."<<endl;
return -1;
}
else {
for (int i=0;i<=S[i]-'0';i++) {
T[i]=S[i];
cout << T[i];
}
cout << endl;
}
cout<<"Copy successfuly!"<<endl;
return 0;
}
int StrEmpty(char *S)
{
if (S[0]=='0') {
cout << "TRUE" << endl;
}
else {
cout<<"FALSE"<<endl;
}
return 0;
}
int StrCompare(char *S,char * T)
{
if (S[0]=='0'||T[0]=='0') {
cout << "The string is empty." << endl;
return -1;
}
int flag = 0;
for (int i = 1; i <= S[0] - '0' && i <= T[0] - '0';i++) {
if (S[i]==T[i])
continue;
else if (S[i]>T[i]) {
flag = 1;
break;
}
else if (S[i]<T[i]) {
flag = -1;
break;
}
}
cout << flag << endl;
return 0;
}
int ClearString(char *S)
{
if (S[0]=='0') {
cout<<"The string is empty."<<endl;
return -1;
}
for (int i=1;i<=S[0]-'0';i++) {
S[i]=' ';
}
cout<<"Clear Successfully."<<endl;
return 0;
}
int Concat(char *str,char *V)
{
if (str[0]=='0') return -1;
int j,i;
for (j=str[0]-'0'+1,i=1;i<=V[0]-'0';j++,i++) {
str[j]=V[i];
}
cout<<"Connect successfully!"<<endl;
str[0]=(str[0]-'0'+V[0]-'0')+'0';
return 0;
}
int SubString(char *sub,char *str,int pos,int len)
{
if (str[0]=='0') {
cout<<"String is empty."<<endl;
return -1;
}
else if (pos<1||pos+len-1>str[0]-'0') {
cout<<"Position is illegal."<<endl;
return -1;
}
for (int i = pos; i <= len;i++) {
sub[i] = str[i];
cout << sub[i];
}
cout << endl;
return 0;
}
int KMP(char *s,char *v,int pos)
{
if (s[0]=='0'||v[0]=='0') {
cout<<"The string is empty."<<endl;
return -1;
}
int i=1;
int nextval[100];
nextval[1]=0;
int j=0;
while (i<=v[0]-'0') {
if (j==0||v[i]==v[j]) {
i++;j++;
if (v[i]!=v[j])
nextval[i]=j;
else
nextval[i]=nextval[j];
}
else {
j=nextval[j];
}
}
i=pos;j=1;
while (i<=s[0]-'0'&&j<=v[0]-'0') {
if (j==0||s[i]==v[j]) {
i++;j++;
}
else j=nextval[j];
}
if (j>v[0]-'0') {
return i-(v[0]-'0');
}
return 0;
}
int PrintAll(char *str)
{
for (int i=1;i<=str[0]-'0';i++) {
cout<<str[i];
}
cout<<endl;
return 0;
}
int Replace(char *s,char *t,char *v)
{
if (s[0]=='0'||t[0]=='0'||v[0]=='0') {
cout<<"The string is empty."<<endl;
return -1;
}
int i = 1,pos;
int len_cmp = (v[0] - '0') - (t[0] - '0');
//cout << len_cmp << endl;
int flag = 0;
if (len_cmp>0) {//负数的逻辑值为真,所以需要加上大于小于号
flag = 1;
}
else if (len_cmp<0) {
flag = -1;
}
//cout << flag << endl;
len_cmp = abs(len_cmp);
while (i<s[0]-'0') {
pos=KMP(s, t, i);
if (flag<0) {
for (int j = pos+(t[0]-'0'); j <= s[0] - '0';j++) {
s[j - len_cmp] = s[j];
}//移动
for (int j = pos, k = 1; k <= v[0] - '0';k++,j++) {
s[j] = v[k];
}
s[0] = (s[0] - '0' - len_cmp) + '0';//修改主串长短
// cout << s[0] - '0'<<endl;
}
else if (flag>0) {//如果v的长度大于t的长度,需要将主串s向后移动
for (int j = s[0] - '0'; j >= pos; j--) {
s[j + len_cmp] = s[j];
}//move
for (int j = pos, k = 1; k <= v[0] - '0';k++,j++) {
s[j] = v[k];
}
s[0] = (s[0] - '0' + len_cmp) + '0';//修改主串长短
}
else if (flag==0){
for (int j = pos, k = 1; k <= v[0] - '0';k++,j++) {
s[j] = v[k];
}
}
i = pos + (v[0] - '0');
//cout << pos <<" "<< i << endl;
}
cout << "Substitute succeed." << endl;
PrintAll(s);
return 0;
/*
测试数据
1
1
a0
1
0
abababab0
10
c0
1
1
ab0
1
0
abababab0
10
c0
1
1
ab0
1
0
abababab0
10
abc0
*/
}
int StrInsert(char *s,int pos,char *v)
{
int v_len = v[0] - '0';
for (int i = s[0] - '0'; i >= pos;i--) {
s[i + v_len] = s[i];
}
for (int i = pos,j=1; j <= v_len;j++,i++) {
s[i] = v[j];
}
s[0] = s[0] - '0' + v_len + '0';
cout << "Insert successfully!" << endl;
PrintAll(s);
return 0;
}
int StrDelete(char *s,int pos,int len)
{
if (pos<1||pos>s[0]-'0') {
cout << "The position is illegal." << endl;
return -1;
}
else if (len>s[0]-'0') {
cout << "The length is too long." << endl;
return -2;
}
for (int i = pos + len; i <= s[0] - '-';i++)
{
s[i-len] = s[i];
}
s[0] = s[0] - '0' - len + '0';
cout << "Delete successfully!" << endl;
PrintAll(s);
return 0;
}
int main()
{
cout<<"Please enter what you want to do.Zero is exit."<<endl
<<"1.Setting a string."<<endl
<<"2.Copy a string."<<endl
<<"3.Is the string empty?"<<endl
<<"4.Compare with the string A and string B."<<endl
<<"5.Length of the string."<<endl
<<"6.Clearing the string."<<endl
<<"7.Connect two string."<<endl
<<"8.Print the substring."<<endl
<<"9.Search the substring."<<endl
<<"10.Replace the substring."<<endl
<<"11.Insert the substring."<<endl
<<"12.Delete the substring."<<endl
<<"13.Print string."<<endl;
int n,pos,len,num;
char T[100]={'0'},S[100]={'0'},V[100]={'0'};
char sub[100]={'0'};
while (cin>>n&&n) {
switch (n) {
case 1:
cout << "Which do you want to set? T or S? Please enter the 1 or 0 to represent the diffirent string." << endl;
cin >> num;
if (num) {
StrAssign(T);
}
else {
StrAssign(S);
}
break;
case 2:
StrAssign(S);
StrCopy(T,S);
break;
case 3:
cout<<"The 1 represent the string T and the 0 represent the string S. Which string would you like to judge?"<<endl;
cin>>num;
if (num) {
StrEmpty(T);
}
else {
StrEmpty(S);
}
break;
case 4:
StrCompare(S,T);
break;
case 5:
cout<<"The 1 represent the string T and the 0 represent the string S. Which length would you like to print?"<<endl;
cin>>num;
if (num) {
cout<<T[0]-'0'<<endl;
}
else {
cout<<S[0]-'0'<<endl;
}
break;
case 6:
cout<<"The 1 represent the string T and the 0 represent the string S. Which string would you like to clear?"<<endl;
cin>>num;
if (num) {
ClearString(T);
}
else {
ClearString(S);
}
break;
case 7:
cout<<"Please enter a string for connect."<<endl;
StrAssign(V);
Concat(S,V);
break;
case 8:
cout << "Which string do you want to see? The string T or the string S? They are represent the 1 and 0." << endl;
cin >> num;
cout<<"Please enter the position and length."<<endl;
cin>>pos>>len;
if (num) {
SubString(sub, T, pos, len);
}
else {
SubString(sub, S, pos, len);
}
break;
case 9:
StrAssign(V);
cout<<"Where do you want to start?"<<endl;
cin >> num;
cout<<KMP(S,V,num)<<endl;
break;
case 10:
StrAssign(V);
Replace(S,T,V);
break;
case 11:
StrAssign(V);
cout << "Please enter the position\n";
cin >> num;
StrInsert(S, num, V);
break;
case 12:
cout << "Where do you want to start? How long do you want to delete?" << endl;
cin >> pos >> num;
StrDelete(S,pos,num);
break;
case 13:
cout << "Which string do you want to see? The string T or the string S? They are represent the 1 and 0." << endl;
cin >> num;
if (num) {
PrintAll(T);
}
else {
PrintAll(S);
}
break;
}
}
return 0;
}
/*
测试数据
2
xiang0
7
yuan0
14
0
*/
串的基本操作(KMP算法实现)的更多相关文章
- 利用KMP算法解决串的模式匹配问题(c++) -- 数据结构
题目: 7-1 串的模式匹配 (30 分) 给定一个主串S(长度<=10^6)和一个模式T(长度<=10^5),要求在主串S中找出与模式T相匹配的子串,返回相匹配的子串中的第一个字符在主串 ...
- 字符串匹配算法系列一:KMP算法原理
本文主要参考了https://mp.weixin.qq.com/s/rbaPmBejID8-rYui35Snrg的表述,加上部分自己的理解 学习任何算法都要了解该算法解决什么问题?我们看看KMP算法主 ...
- 详讲KMP算法
两个字符串: 模式串:ababcaba 文本串:ababcabcbababcabacaba KMP算法作用:快速在文本串中匹配到模式串 如果是穷举法的方式: 大家有发现,这样比效率很低的. 所以就需要 ...
- 数据结构与算法JavaScript (五) 串(经典KMP算法)
KMP算法和BM算法 KMP是前缀匹配和BM后缀匹配的经典算法,看得出来前缀匹配和后缀匹配的区别就仅仅在于比较的顺序不同 前缀匹配是指:模式串和母串的比较从左到右,模式串的移动也是从 左到右 后缀匹配 ...
- 《数据结构》之串的模式匹配算法——KMP算法
//串的模式匹配算法 //KMP算法,时间复杂度为O(n+m) #include <iostream> #include <string> #include <cstri ...
- 数据结构- 串的模式匹配算法:BF和 KMP算法
数据结构- 串的模式匹配算法:BF和 KMP算法 Brute-Force算法的思想 1.BF(Brute-Force)算法 Brute-Force算法的基本思想是: 1) 从目标串s 的第一个字 ...
- 基础数据结构-串-KMP算法
KMP算法用于模式串字符匹配,因为没有提前预习,上课时听得云里雾里,后来回去看了一晚上,翻了一些网上的讲解才理解了.我简单讲一下,我们在一串字符串A里搜索匹配另一段字符串B时,思路最简单方法的就是从第 ...
- 串的模式匹配和KMP算法
在对字符串的操作中,我们经常要用到子串的查找功能,我们称子串为模式串,模式串在主串中的查找过程我们成为模式匹配,KMP算法就是一个高效的模式匹配算法.KMP算法是蛮力算法的一种改进,下面我们先来介绍蛮 ...
- 第4章学习小结_串(BF&KMP算法)、数组(三元组)
这一章学习之后,我想对串这个部分写一下我的总结体会. 串也有顺序和链式两种存储结构,但大多采用顺序存储结构比较方便.字符串定义可以用字符数组比如:char c[10];也可以用C++中定义一个字符串s ...
- 问题 1690: 算法4-7:KMP算法中的模式串移动数组
题目链接:https://www.dotcpp.com/oj/problem1690.html 题目描述 字符串的子串定位称为模式匹配,模式匹配可以有多种方法.简单的算法可以使用两重嵌套循环,时间复杂 ...
随机推荐
- Swift3.0 字符串(string)
string常用的一些操作方式 //字符串 //1.初始化字符串 //1.1通过字面量赋值的方式初始化字符串 let tempStrig = "this is temp string&quo ...
- Codeforces626B - Cards【模拟】
题意: 两张相同可以合并成相同: 两张不同可以产生另外一个不同: 求最终的可能颜色: 思路: 模拟啊. 总共也就那么几种情况,具体看挫code--. #include<iostream> ...
- hdoj5698
果然以前不想搞的东西,今天他妈全来了,我要爆炸,除了说操....真是欲哭无泪啊..... //这道题目卡在逆元了.... //利用逆元计算1/(n!(m-n)!) //对于正整数a,m如果有ax≡1( ...
- 基于FBX SDK的FBX模型解析与加载 -(三)
http://blog.csdn.net/bugrunner/article/details/7229416 6. 加载Camera和Light 在FBX模型中除了几何数据外较为常用的信息可能就是Ca ...
- pycharm 添加个人信息
2. 可以使用搜索快速找到"File and Code Templates", 右侧菜单选择"Python Script",对模板进行编辑 格式为: ${< ...
- 两边是线 ,中间是文字 的CSS写法 而且还是自适应的
- April Fools Contest 2017 F
Description You are developing a new feature for the website which sells airline tickets: being able ...
- linux之lamp环境的搭建
linux之lamp环境的搭建 1.安装lamp环境的安装工具 我们的lamp环境都是采用源码包进行编译安装: 编译安装需要工具gcc gcc-c++. 建议在线联网安装:yum -install ...
- 接口测试01 - HTTP协议报文结构及示例
HTTP基本架构 用一张简单的流程图来展示HTTP协议的基本架构,以便先有个基础的了解. 1)Web Client可以是浏览器.搜索引擎等等一切基于HTTP协议发起http请求的工具. 2)Web S ...
- Finally语句