#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. (水题)洛谷 - P2089 - 烤鸡

    https://www.luogu.org/problemnew/show/P2089 非常暴力的dfs,不知道不剪枝会怎么样,但是其实最多也就 $3^{10}$ ,大不到哪里去.还有一个细节就是大于 ...

  2. HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)

    传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...

  3. 7天学完Java基础之3/7

    API概述 什么叫做API? API(Application Programming lnterface),应用程序编程接口. 所谓API就是值好多的类,好多的方法,JDK给我们提供了很多现成的类,我 ...

  4. Caffe实战一(环境准备及CPU模式下编译)

    经过前几天的折腾,终于把Ubuntu16.04开发环境给搭建了起来,包括win10+Ubuntu双系统的安装.系统安装后的优化等等. 详见之前的文章:Ubuntu16.04.2 LTS 64bit系统 ...

  5. Codeforces Round #418 (Div. 2) A

    Description A few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight ...

  6. android动画(3)layout动画,layoutChanged动画及算定义它,ListViewActivity的Layout动画(代码和xm配置两种实现l)

    1.layout切换动画 代码: 本示例是fragment切换.在它的oncreateView中 public class LayoutAnimationFrgmt extends Fragment ...

  7. 纯js手动分页

    昨天让做个页面,后台提供所有数据,没有做好分页,需要前端js手动分页. 我参考了 http://www.cnblogs.com/jiechn/p/4095029.html 做了些许改动让分页效果更加完 ...

  8. B. Code For 1 一个类似于线段树的东西

    http://codeforces.com/contest/768/problem/B 我的做法是,观察到,只有是x % 2的情况下,才有可能出现0 其他的,都是1来的,所以开始的ans应该是R - ...

  9. 单页Html及Android App供小孩学习常用汉字

    为了检验及帮助小孩学习常用汉字,简单开发本网页应用: 常用汉字是按使用频率排序的,来源于网上: 该简单应用 有Android APP下载 “学习常用汉字_20150910.apk” 单页Html 示例 ...

  10. PE刷题记

    PE 中文翻译 最喜欢做这种很有意思的数学题了虽然数学很垃圾 但是这个网站的提交方式好鬼畜啊qwq 1.Multiples of 3 and 5 直接枚举 2.Even Fibonacci numbe ...