字符串复习:
eg4.1 特殊乘法(清华大学复试上机题)
//特殊乘法
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1,s2;
while(cin>>s1>>s2)
{
int res=0;
for(int i=0;i<s1.size();i++)
{
for(int j=0;j<s2.size();j++)
res+=(s1[i]-'0')*(s2[j]-'0');
}
cout<<res<<endl;
}
return 0;
}
eg4.2 密码翻译(北京大学复试上机题)
对给定的字符串,将其中从a-y的字母和从A-Y的字母用后续字母替代,z和Z用a和A替代,得到一个简单的加密字符串
代码如下:
//密码翻译
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
while(getline(cin,s))
{
for(int i=0;i<s.size();i++)
{
if((s[i]>='a'&&s[i]<'z')||(s[i]>='A'&&s[i]<'Z'))
s[i]++;
else if(s[i]=='z') s[i]='a';
else if(s[i]=='Z') s[i]='A';
else s[i]+=0;
}
cout<<s<<endl;
}
return 0;
}
eg4.3 简单密码
//简单密码
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
string s;
while(getline(cin,s))//开始行
{
if(s=="ENDOFINPUT") break;
getline(cin,s);//密文
for(int i=0;i<s.size();i++)
{
if(s[i]>='A'&&s[i]<='Z')
{
s[i]=(s[i]-'A'+21)%26+'A';
}
else s[i]+=0;
}
cout<<s<<endl;
getline(cin,s);//结束行
}
return 0;
}
eg4.4 统计字符(浙江大学复试上机题)
#include <bits/stdc++.h>
#include <ios>
using namespace std;
int nums[128];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
string str1,str2;
while(getline(cin,str1))//读入第一行字符
{
memset(nums,0,sizeof nums);
if(str1=="#") break;
getline(cin,str2);
for(int i=0;i<str2.size();i++)
{
nums[str2[i]]++;
}
for(int i=0;i<str1.size();i++)
{
cout<<str1[i]<<" "<<nums[str1[i]]<<endl;
}
}
return 0;
}
eg4.5 字母统计(上海交通大学复试上机题)
#include <bits/stdc++.h>
using namespace std;
int nums[128];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
string str;
cin>>str;
for(int i=0;i<str.size();i++)
{
nums[str[i]]++;
}
for(int i=65;i<=90;i++)
{
cout<<char(i)<<":"<<nums[i]<<endl;
}
}
ex4.1 skew数(北京大学复试上机题)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
string str1;
while(cin>>str1)
{
LL res=0;
LL tmp=1;
for(int i=str1.size()-1;i>=0;i--)
{
tmp*=2;
tmp--;
res+=(tmp*(LL)(str1[i]-'0'));
tmp++;
}
cout<<res<<endl;
}
return 0;
}
ex4.2 单词替换(北京大学复试上机题)学习到了字符串常用函数insert/erase/substr/find的基本用法
#include <bits/stdc++.h>
#include <ios>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
string s,a,b;
getline(cin,s);
getline(cin,a);
getline(cin,b);
s=" "+s+" ";
a=" "+a+" ";
b=" "+b+" ";
while(1)
{
int index=s.find(a);
if(index==string::npos) break;
else
{
s.erase(index,a.size());
s.insert(index,b);
}
}
int n=s.size();
cout<<s.substr(1,n-2)<<endl;
}
ex4.3 首字母大写(北京大学复试上机题)
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
string s;
getline(cin,s);
if((int)s[0]>=97&&(int)s[0]<=122)
s[0]-=32;
for(int i=1;i<s.size();i++)
{
if(s[i]==' '||s[i]=='\t'||s[i]=='\r'||s[i]=='\n')
{
if((int)s[i+1]>=97&&(int)s[i+1]<=122)
s[i+1]-=32;
}
}
cout<<s<<endl;
return 0;
}
ex4.4 浮点数加法
今天正好复习数据结构,结合之前在代码随想录上对kmp算法的描述,重新反刍一下kmp算法
4.1串的定义和实现
字符串简称串,串是由零个或多个字符组成的有限序列。一般记为 S='a1a2...an'
串中字符的个数n称为串的长度。n=0时的空串称为空串
串中的任意多个连续的字符组成的子序列称为该串的子串,包含子串的串称为主串。由一个或者多个空格组成的串称为空格串。
4.2 串的模式匹配
子串的定位操作通常称为串的模式匹配,它求的是子串(模式串)在主串中的位置。
4.2.1 简单的模式匹配算法
算法思想:从S的第一个字符起,与模式T的第一个字符比较,若相等,则继续逐个比较后续字符;否则从主串的下一个字符起,重新和模式的字符串比较。以此类推,直至模式T中的每个字符依次和主串S中的一个连续的字符序列相等,则匹配成功。
该算法的最坏时间复杂度为O(nm),n和m分别为主串和模式串的长度
4.2.2 串的模式匹配算法——KMP算法
前缀、后缀和部分匹配值。前缀指的是除了最后一个字符以外,字符串所有的头部子串
后缀指的是除了第一个字符外,字符串的所有尾部子串
部分匹配值则为字符串前缀和后缀的最长相等前后缀长度。
按照下面的公式计算出子串需要向后移动的位数:
移动位数=已匹配的字符数-对应的部分匹配值
这样主串就不用回溯,该算法的时间复杂度为O(m+n)
leetcode28题:实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
示例 1: 输入: haystack = "hello", needle = "ll" 输出: 2
示例 2: 输入: haystack = "aaaaa", needle = "bba" 输出: -1
说明: 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
KMP算法可以运行的源代码如下图所示
#include <bits/stdc++.h>
using namespace std;
void getNext(int *next,string s)
{
int j=0;
next[0]=0;
for(int i=1;i<s.size();i++)
{
while(j>0&&s[i]!=s[j])
j=next[j-1];
if(s[i]==s[j])
j++;
next[i]=j;
}
}
int kmp(string haystack,string needle)
{
if(needle.size()==0) return 0;
int next[needle.size()];
getNext(next,needle);
int j=0;
for(int i=0;i<haystack.size();i++)
{
while(j>0&&haystack[i]!=needle[j])
j=next[j-1];
if(haystack[i]==needle[j])
j++;
if(j==needle.size())
return (i-needle.size()+1);
}
return -1;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
string s1,s2;
cin>>s1>>s2;
cout<<kmp(s1,s2)<<endl;
return 0;
}
注意KMP算法还可以编译原理中的状态机知识来理解呢
leetcode459题 重复的子字符串
#include <bits/stdc++.h>
using namespace std;
void getNext(int* next,string s)
{
next[0]=0;
int j=0;
for(int i=1;i<s.size();i++)
{
while(j>0&&s[i]!=s[j]) j=next[j-1];
if(s[i]==s[j]) j++;
next[i]=j;
}
}
bool check(string s)
{
if(s.size()==0)
return false;
int next[s.size()];
getNext(next,s);
int len=s.size();
//则说明数组的长度正好可以被 (数组长度-最长相等前后缀的长度)整除,说明该字符串有重复的子字符串
if(next[len-1]!=0&&len%(len-(next[len-1]))==0)
return true;
return false;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
string s;
cin>>s;
if(check(s)) cout<<"yes"<<endl;
else cout<<"no"<<endl;
return 0;
}