说明:
都是字符串的基本操作没啥好说的,直接上题目和代码了。
题目列表:
问题 A: 字符串翻转
参考题解:
#include <iostream>
#include <string>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
std::string s;cin >> s;
std::reverse(s.begin(),s.end());
cout << s << '\n';
return 0;
}
问题 B: 字符串联接
参考题解:
#include <iostream>
#include <string>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
std::string a,b;cin >> a >> b;
std::string s = a+b;
cout << s << endl;
return 0;
}
问题 C: 三个字符串排序
参考题解:
#include <iostream>
#include <string>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
string a[4];
for(int i = 1;i<=3;i++) cin >> a[i];
sort(a+1,a+4);
cout << a[1] << '\n' << a[2] << '\n' << a[3] << '\n';
return 0;
}
问题 D: 字符串匹配
参考题解:
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using std::cin;
using std::cout;
using std::string;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
string buf,s;
int q,op,len;
auto brute_force = [&]()->int{
int ans = 0;
for(int i = 0;i<buf.size();i++){
for(int j = 0;j<s.size();j++){
int k;
for(k = 0;k<s.size();k++){
if(buf[i+k]!=s[j+k]) break;
if(i+k==buf.size()) break;
if(j+k==s.size()) break;
}
ans = std::max(ans,k);
}
}
return ans;
};
while(cin >> buf){
cin >> q;
while(q--){
cin >> op;
if(op==1){
cin >> s;
buf = buf+s;
}else if(op==2){
cin >> s;
cout << brute_force() << '\n';
}else if(op==3){
cin >> len;
buf = buf.substr(0,buf.size()-len);
}
}
}
return 0;
}
问题 E: 字符串奇数位置右移
参考题解:
#include <iostream>
#include <string>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
string s;cin >> s;
if(s.size()&1){
char tmp = s[s.size()-1];
for(int i = s.size()-1;i>=2;i-=2){
s[i] = s[i-2];
}
s[0] = tmp;
}else{
char tmp = s[s.size()-2];
for(int i = s.size()-2;i>=2;i-=2){
s[i] = s[i-2];
}
s[0] = tmp;
}
cout << s << endl;
return 0;
}
问题 F: 字符串漂移
参考题解:
#include <iostream>
#include <string>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
string s;getline(cin,s);
int len = s.size();
s = ' '+s;
for(int i = 1;i<=len;i++){
if(s[i]>='a'&&s[i]<='z'){
int k = i%26;
if(s[i]-'a'<k){
s[i] = 'z' - (k-s[i]+'a')+1;
}else s[i] = s[i] - k;
}
}
cout << s.substr(1) << endl;
return 0;
}
问题 G: 输出亲朋字符串
参考题解:
#include <iostream>
#include <string>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
string s;cin >> s;
char tmp = s[0];
for(int i = 0;i<s.size()-1;i++){
s[i] = s[i]+s[i+1];
}
s[s.size()-1] = s[s.size()-1]+tmp;
cout << s << endl;
return 0;
}
问题 H: 找子串
参考题解:
#include <iostream>
#include <string>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
string s,t;
while(cin >> s >> t){
if(s.find(t)==string::npos) cout << "No\n";
else cout << "Yes\n";
}
return 0;
}
问题 I: 字符串替换
参考题解:
#include <iostream>
#include <string>
#include <map>
using std::cin;
using std::cout;
using std::string;
using std::map;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
string s,t,str;cin >> s >> t >> str;
string ans = s;
int pos;
while((pos=ans.find(t))!=string::npos){
ans.replace(pos,t.size(),str);
}
cout << ans << std::endl;
return 0;
}
问题 J: 字符串复制(程序填空)
参考题解1(你让我填空我就填空嘛?(bushi)):
#include <iostream>
#include <string>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
string s;getline(cin,s);
int m;cin >> m;
string ans = s.substr(m);
cout << s << '\n' << ans << endl;
return 0;
}
参考题解2(程序填空):
#include<iostream>
using namespace std;
int main()
{
int m;
char str1[51],str2[51];
gets(str1);
// ______(1)_______
cin>>m;
char *p1,*p2;
p1=str1+m;
// ______(2)_______
p2=str2;
while(*p1)
*(p2++)=*(p1++);
*p2='\0';
puts(str1);
puts(str2);
// ______(3)_______
return 0;
}
问题 K: 查找子串出现次数
参考题解:
#include <iostream>
#include <string>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
string s,t;
cin >> s >> t;
int k = 0,ans = 0;
while(s.find(t,k)!=string::npos){
k = s.find(t,k)+t.size();
ans++;
}
cout << ans << endl;
return 0;
}
问题 L: 回文串游戏
参考题解:
#include <iostream>
#include <string>
#include <map>
using std::cin;
using std::cout;
using std::string;
using std::map;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
string s;cin >> s;
map<char,int> mp;
for(auto &c:s) mp[c]++;
int ans = 0;
for(auto &i:mp){
if(i.second&1) ans++;
}
cout << (ans==0||ans&1?"First":"Second") << std::endl;
return 0;
}
问题 M: 虎哥找子串
参考题解:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3e5+5;
string s;
ll ans = 0;
void solve(){
cin >> s;
for(int i = 0;i<s.size();i++){
if(i==0){
if((s[i]^48)%4==0) ans++;
}else{
if((s[i]^48)%4==0){
if((s[i-1]^48)%2==1) ans++;
else ans+=(i+1);
}else if((s[i]^48)%4==2){
if((s[i-1]^48)%2==1) ans+=i;
}
}
}
cout << ans << '\n';
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
int T = 1;
while(T--) solve();
return 0;
}