3. 字符串

string

构造

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;



int main() {

	string s0 = "Initial String";
	string s1;
	string s2(s0);
	string s3(s0, 8, 3);//Str
	string s4(" A character sequence");
	string s5("Another character sequence", 12); //Another char
	string s6(10, 'x'); //xxxxxxxxxx

	cout << "s0: " << s0 << endl;
	cout << "s1: " << s1 << endl;
	cout << "s2: " << s2 << endl;
	cout << "s3: " << s3 << endl;
	cout << "s4: " << s4 << endl;
	cout << "s5: " << s5 << endl;
	cout << "s6: " << s6 << endl;

	return 0;

}

操作

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;

int main() {
	string str = "hello world";
	for (int i = 0; i < str.size(); i++) {
		cout << str[i];
	}
	cout << endl << "the 7th element of str is: " << str[6] << endl;

	string str1 = "to be question";
	string str2 = "that is a ";
	string str3 = "or not world";
	string str4;

	str4.insert(0, str1); //to be question
	str4.insert(6, str3, 0, 7); //to be or not question
	str4.insert(13, "to be "); // to be or not to be question
	str4.insert(19, str2); //to be or not to be that is a question
	cout << "str4: " << str4 << endl;

	str4.erase(19);//to be or not to be
	str4.erase(0, 9); //not to be
	cout << "str4: " << str4 << endl;

	str4.clear();
	cout << "str4: " << str4 << endl;


	return 0;

}

运算

函数

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

int main()
{
    string str1 = "Hello World, End World";
    int length=str1.size();
    cout<<length<<endl;
    
    int position1=str1.find("World");//6
    int position2=str1.find("World",10);//17
    cout<<position1<<endl<<position2<<endl;
    
    position1=str1.find('l');//2
    position2=str1.find('l',10);//20
    cout<<position1<<endl<<position2<<endl;
    
    position1=str1.find('a');//-1
    cout<<position1<<endl;
    
    string str2=str1.substr(13);//End World
    string str3=str1.substr(13,3);//End
    cout<<str2<<endl<<str3<<endl;
    return 0;
}

遍历

特殊乘法

#include <iostream>
#include <stdio.h>
#include <string>//c
#include<string.h>//c
using namespace std;

int main()
{
    string str1,str2;
    while(cin>>str1>>str2){
		int answer=0;
		for(int i=0;i<str1.size();i++){
			for(int j=0;j<str2.size();j++){
				answer+=(str1[i]-'0')*(str2[j]-'0');
			}
		}
		cout<<answer<<endl;
	}
    return 0;
}

加密

简单密码

#include <iostream>
#include <stdio.h>
#include <string>//c
using namespace std;

int main() {
//	char c = 'A';
//	cout << (c - 'A' - 5 + 26) + 'A' << endl;

	string str;
	while (getline(cin, str)) {
		if (str == "ENDOFINPUT") {
			break;
		}
		//接受密文
		getline(cin, str);
		for (int i = 0; i < str.size(); i++) {
			if ('A' <= str[i] && str[i] <= 'Z') {
				str[i] = (str[i] - 'A' - 5 + 26) % 26 + 'A';
			}
		}
		cout << str << endl;
		getline(cin, str);//接受END 

	}

	return 0;
}

统计

统计字符串

#include <iostream>
#include <stdio.h>
#include <string>//c
#include<cstring>
using namespace std;
int number[128];
int main() {

	string str1, str2;
	while (getline(cin, str1)) {
		if (str1 == "#") {
			break;
		}
		memset(number, 0, sizeof(number));
		getline(cin, str2);
		for (int i = 0; i < str2.size(); i++) {
			number[str2[i]]++;
		}
		for (int i = 0; i < str1.size(); i++) {
			printf("%c %d\n", str1[i], number[str1[i]]);
		}
	}
	return 0;
}

匹配

  • Detection 是否出现
  • Location 何处出现
  • Count 出现几次
  • Enumeration 每次出现在何处

蛮力算法

朴素模式匹配算法的缺点:当某些子串与模式串能部分匹配时,主串的扫描指针i 经
常回溯,导致时间开销增加。最坏时间复杂度O(nm)

KMP算法

改进思路:主串指针不回溯,只有模式串指针回溯

KMP算法:当子串和模式串不匹配时,主串指针i 不回溯,模式串指针j=next[j]
算法平均时间复杂度:O(n+m)

串的前缀:包含第一个字符,且不包含最后一个字符的子串
串的后缀:包含最后一个字符,且不包含第一个字符的子串

next数组:当模式串的第j个字符匹配失败时,令模式串跳到next[j]再继续匹配

**next数组手算方法:**当第j个字符匹配失败,由前1~j-1 个字符组成的串记为S,则:
next[j]=S的最长相等前后缀长度+1,特别地,next[0]=-1;

#include <iostream>
#include <stdio.h>
#include <string>//c
#include<cstring>
using namespace std;
//next数组:当模式串的第j个字符匹配失败时,令模式串跳到next[j]再继续匹配 
const int MAXN=100;
int nextTable[MAXN] ;

void getNextTable(string pattern){
	int m=pattern.size();
	int j=0;
	nextTable[j]=-1;
	int t=nextTable[j];
	while(j<m){
		if(t==-1||pattern[t]==pattern[j]){
			t++;
			j++;
//			nextTable[j+1]=nextTable[j]+1;
			nextTable[j]=t;
		}
		else{
			t= nextTable[t];
		}
	}
	return;
}

int KMP(string text, string pattern) {
	getNextTable(pattern);
	int n = text.size();
	int m = pattern.size();
	int i = 0;
	int j = 0;
	while (i < n && j < m) {
		if (j == -1 || text[i] == pattern[j]) {
			i++;
			j++;
		} else {
			j = nextTable[j];
		}
	}
	if(j==m){
		return i-m;//匹配成功的起始位置 
	}
	else{
		return -1;
	}
}

int main() {
	string text;
	string pattern;
	text="I love you";
	pattern="love";
	int position=KMP(text,pattern);
	cout<<position<<endl;
	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ROJS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值