#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算法实现)的更多相关文章

  1. 利用KMP算法解决串的模式匹配问题(c++) -- 数据结构

    题目: 7-1 串的模式匹配 (30 分) 给定一个主串S(长度<=10^6)和一个模式T(长度<=10^5),要求在主串S中找出与模式T相匹配的子串,返回相匹配的子串中的第一个字符在主串 ...

  2. 字符串匹配算法系列一:KMP算法原理

    本文主要参考了https://mp.weixin.qq.com/s/rbaPmBejID8-rYui35Snrg的表述,加上部分自己的理解 学习任何算法都要了解该算法解决什么问题?我们看看KMP算法主 ...

  3. 详讲KMP算法

    两个字符串: 模式串:ababcaba 文本串:ababcabcbababcabacaba KMP算法作用:快速在文本串中匹配到模式串 如果是穷举法的方式: 大家有发现,这样比效率很低的. 所以就需要 ...

  4. 数据结构与算法JavaScript (五) 串(经典KMP算法)

    KMP算法和BM算法 KMP是前缀匹配和BM后缀匹配的经典算法,看得出来前缀匹配和后缀匹配的区别就仅仅在于比较的顺序不同 前缀匹配是指:模式串和母串的比较从左到右,模式串的移动也是从 左到右 后缀匹配 ...

  5. 《数据结构》之串的模式匹配算法——KMP算法

    //串的模式匹配算法 //KMP算法,时间复杂度为O(n+m) #include <iostream> #include <string> #include <cstri ...

  6. 数据结构- 串的模式匹配算法:BF和 KMP算法

      数据结构- 串的模式匹配算法:BF和 KMP算法  Brute-Force算法的思想 1.BF(Brute-Force)算法 Brute-Force算法的基本思想是: 1) 从目标串s 的第一个字 ...

  7. 基础数据结构-串-KMP算法

    KMP算法用于模式串字符匹配,因为没有提前预习,上课时听得云里雾里,后来回去看了一晚上,翻了一些网上的讲解才理解了.我简单讲一下,我们在一串字符串A里搜索匹配另一段字符串B时,思路最简单方法的就是从第 ...

  8. 串的模式匹配和KMP算法

    在对字符串的操作中,我们经常要用到子串的查找功能,我们称子串为模式串,模式串在主串中的查找过程我们成为模式匹配,KMP算法就是一个高效的模式匹配算法.KMP算法是蛮力算法的一种改进,下面我们先来介绍蛮 ...

  9. 第4章学习小结_串(BF&KMP算法)、数组(三元组)

    这一章学习之后,我想对串这个部分写一下我的总结体会. 串也有顺序和链式两种存储结构,但大多采用顺序存储结构比较方便.字符串定义可以用字符数组比如:char c[10];也可以用C++中定义一个字符串s ...

  10. 问题 1690: 算法4-7:KMP算法中的模式串移动数组

    题目链接:https://www.dotcpp.com/oj/problem1690.html 题目描述 字符串的子串定位称为模式匹配,模式匹配可以有多种方法.简单的算法可以使用两重嵌套循环,时间复杂 ...

随机推荐

  1. 洛谷 - P1217 - 回文质数 - 枚举

    https://www.luogu.org/problemnew/show/P1217 考虑暴力生成所有的回文数然后再判断是不是质数.注意个位的选择实际上只有4种.所以是 $4*10^3*10^3=4 ...

  2. bzoj 4197: [Noi2015]寿司晚宴【状压dp】

    一个数内可能多个的质因数只有小于根号n的,500内这样的数只有8个,所以考虑状压 把2~n的数处理出小于根号500的质因数集压成s,以及大质数p(没有就是1),然后按p排序 根据题目要求,拥有一个质因 ...

  3. $BREEZE'S Diary$

    蒟蒻的日记没什么好看的. 2019-01-28 期末砸了. 洛谷开创小号. 开创博客园. 2019-01-29 坐标:义乌中学 咱今天又来义乌中学受虐了 感谢hjf给咱一次爆0的机会 题解 2019- ...

  4. java sevlet Session

    * 如果浏览器支持Cookie,创建Session的时候会把SessionId保存在Cookie中 * 否则必须自己编程使用URL重写的方式实现Session:response.encodeURL()

  5. python之list操作

    序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. 列表(list)是最常用的Python数据类型,它可以作为一个方 ...

  6. IDEA远程调试hadoop程序

    远程调试Hadoop各组件 Hadoop学习之配置Eclipse远程调试Hadoop IDEA远程调试hadoop Hadoop 研发之远程调试详细剖析--WordCount V2.0 eclipse ...

  7. linux查找命令(find)

    linux查找命令(find) 命令格式: find [目录] [选项] [选项的条件] 选项: -name:文件名称查找 -size:文件的大小来查找 -perm:文件的权限来查找 ①根据文件的名称 ...

  8. 转 做了两款数据库监控工具(mysql and nosql),打算在近期开源

    http://www.cnblogs.com/leefreeman/p/7297549.html 监控指标:https://www.linuxidc.com/Linux/2015-08/122009. ...

  9. JAVA常用知识总结(二)

    JAVA中的参数传递总结先看两道笔试题: public class Test2 { public static void main (String [] args) { StringBuffer a ...

  10. [转]ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL

    本文转自:http://www.cnblogs.com/John-Connor/archive/2012/05/03/2478821.html 引言-- 在初级篇中,我们介绍了如何利用基于ASP.NE ...