字符串专题

A.首字母大写

#include <bits/stdc++.h>
using namespace std;

int main() {
	string s;
	getline(cin, s);
	for(int i = 0; i < s.size(); i ++) {
		if(i == 0 && isalpha(s[i])) s[i] = toupper(s[i]);
		if(!isalpha(s[i - 1]) && isalpha(s[i])) s[i] = toupper(s[i]);
	}
	cout << s << endl;
} 

B.验证子串

#include <bits/stdc++.h>
using namespace std;

int main() {
	string s1;
	string s2;
	cin >> s1 >> s2;
	if(s1.find(s2) != -1)
		cout << s2 << " is substring of " << s1 << endl;
	else if(s2.find(s1) != -1) 
		cout << s1 << " is substring of " << s2 << endl;
	else cout << "No substring" << endl;
	return 0;
}  

C.大小写转换

#include <bits/stdc++.h>
using namespace std;

int main() {
	string a;
	while(cin >> a) {
		for(int i = 0; i < a.size(); i ++) {
			if(islower(a[i])) a[i] = toupper(a[i]);
		}
		cout << a << endl;
	}
	return 0;
}  

D.Text Reverse


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;


int main() {
	int n;
	cin >> n;
	getchar();
	while(n --) {
		string s;
		getline(cin, s);
		int lst = 0, cur = 0, i;
		string tmp;
		for(i = 0; i < s.size(); i ++) {
			if(s[i] == ' ') {
				cur = i;
				tmp = s.substr(lst, cur - lst);
				lst = cur + 1;
				reverse(tmp.begin(), tmp.end());
				cout << tmp << ' ';
			}
		}
		tmp = s.substr(cur + 1, i - cur);
		reverse(tmp.begin(), tmp.end());
		cout << tmp << endl;
	}
	return 0;
}  

E.排序

#include <stdio.h>
#include <string.h>
typedef long long ll;
const int N = 10001;

char s[N];
ll a[N];

int main() {
	while(~scanf("%s", s)) {
		int f = 0, idx = 0;
		ll tmp = 0;
		memset(a, 0, sizeof a);
		for(int i = 0; s[i]; i ++) {
			if(s[i] == '5') {
				if(f) a[idx ++] = tmp, f = 0;
				tmp = 0;
				continue;
			}
			tmp = tmp * 10 + s[i] - '0';
			f = 1;
		}
		if(s[strlen(s) - 1] != '5')
			a[idx ++] = tmp;
		for(int i = 1; i < idx; i ++) {
			for(int j = 0; j < idx - i; j ++) {
				if(a[j] > a[j + 1]) {
					int t = a[j];
					a[j] = a[j + 1];
					a[j + 1] = t;
				}
			}
		}
		printf("%lld", a[0]);
		for(int i = 1; i < idx; i ++) printf(" %lld", a[i]);
		puts("");
	}
	return 0;
}

F.What Are You Talking About

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
using namespace std;

map<string, string>mymap;
vector<string> vs;

int main() {
	string s, s2;
	while(cin >> s && s != "END") {
		if(s == "START") continue;
		cin >> s2;
		mymap[s2] = s;
	}
	getchar();
	while(getline(cin, s) && s != "END") {
		if(s == "START") continue;
		s2 = "";
//		cout << s << endl;
		for(int i = 0; i < s.size(); i ++) {
			if(isalpha(s[i])) s2 += s[i];
			else {
				if(mymap[s2] != "") cout << mymap[s2];
				else cout << s2;
				s2 = "";
				cout << s[i];
			}
			
		}
		if(mymap[s2] != "") cout << mymap[s2];
		else cout << s2;
		cout << endl;
	}
}

G.AC Me

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;


int main() {
	string s;
	while(getline(cin, s)) {
		int a[26] = {0};
		for(int i = 0; i < s.size(); i ++) a[s[i] - 'a'] ++;
		for(int i = 0; i < 26; i ++) {
			cout << (char)(i + 97) << ':' << a[i] << endl;
		}
		cout << endl;
	}
	
	return 0;
}  

H.剪花布条

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;


int main() {
	string s1, s2;
	while(cin >> s1, s1 != "#") {
		cin >> s2;
		int cont = 0;
		int len2 = s2.size();
		for(int i = 0; i < s1.size(); i ++) {
			if(s1.substr(i, len2) == s2) cont ++, i += len2 - 1;
		}
		cout << cont << endl;
	}
	return 0;
}  

I.子串查找

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

const int N = 1000010;

char s[N], p[N];
int ne[N];

int main() {
	scanf("%s %s", s + 1, p + 1);
	int cont = 0;
	int m = strlen(p + 1);
	int n = strlen(s + 1);
	for(int i = 2, j = 0; i <= m; i ++) {
		while(j && p[i] != p[j + 1]) j = ne[j];
		if(p[i] == p[j + 1]) j ++;
		ne[i] = j;
	}
	for(int i = 1, j = 0; i <= n; i ++) {
		while(j && s[i] != p[j + 1]) j = ne[j];
		if(s[i] == p[j + 1]) j ++;
		if(j == m) j = ne[j], cont ++;
	}
	cout << cont << endl;
	return 0;
}

J.字符串最大跨距

#include <bits/stdc++.h>
using namespace std;
char s[500], s1[20], s2[20];

int main() {
	string str;
	cin >> str;
	string s, s1, s2;
	int pos1, pos2;
	for(int i = 0; i < str.size(); i ++)
		if(str[i] == ',') {
			pos1 = i;
			break;
		}
	for(int i = str.size() - 1; i >= 0; i --)
		if(str[i] == ',') {
			pos2 = i;
			break;
		}
	s = str.substr(0, pos1);
	s1 = str.substr(pos1 + 1, pos2 - pos1 - 1);
	s2 = str.substr(pos2 + 1, str.size() - pos2);
	int p1, p2;
	int f1 = 0, f2 = 0;
	p1 = s.find(s1) + s1.size();
	f1 = s.find(s1);
	reverse(s.begin(), s.end());
	reverse(s2.begin(), s2.end());
	p2 = s.find(s2) + s2.size();
	f2 = s.find(s2);
	p2 = s.size() - p2;
	if(p2 - p1 < 0 || f1 == -1 || f2 == -1) cout << -1 << endl;
	else cout << p2 - p1 << endl;
	return 0;
}

K.Cyclic Nacklace

#include <iostream>
#include <cstring>
using namespace std;

const int N = 100010;

int ne[N];
char s[N];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T = 1;
	cin >> T;
	while(T --) {
//		memset(ne, 0, sizeof ne);
//		memset(s, 0, sizeof s);
		cin >> (s + 1);
		int m = strlen(s + 1);
		for(int i = 2, j = 0; i <= m; i ++) {
			while(j && s[i] != s[j + 1]) j = ne[j];
			if(s[i] == s[j + 1]) j ++;
			ne[i] = j;
		}
		int l = m - ne[m];
//		cout << l << endl;
		if(l == m) cout << m << endl;
		else {
			if(m % l == 0) cout << 0 << endl;
			else cout << l - m % l << endl;
		}
	}
	return 0;
}

L.最长回文

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string Manacher(string s)
{
	/*改造字符串*/
	string res="$#";
	for(int i=0;i<s.size();++i)
	{
		res+=s[i];
		res+="#";
	}
	
	/*数组*/
	vector<int> P(res.size(),0);
	int mi=0,right=0;   //mi为最大回文串对应的中心点,right为该回文串能达到的最右端的值
	int maxLen=0,maxPoint=0;    //maxLen为最大回文串的长度,maxPoint为记录中心点
	
	for(int i=1;i<res.size();++i)
	{
		P[i]=right>i ?min(P[2*mi-i],right-i):1;     //关键句,文中对这句以详细讲解
		
		while(res[i+P[i]]==res[i-P[i]])
			++P[i];
		
		if(right<i+P[i])    //超过之前的最右端,则改变中心点和对应的最右端
		{
			right=i+P[i];
			mi=i;
		}
		
		if(maxLen<P[i])     //更新最大回文串的长度,并记下此时的点
		{
			maxLen=P[i];
			maxPoint=i;
		}
	}
	return s.substr((maxPoint-maxLen)/2,maxLen-1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	string s;
	while(cin >> s) {
		string s2 = Manacher(s);
		cout << s2.size() << endl;
	}
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java中的字符串是不可变的,即每当对字符串进行更改时,都会创建一个新的实例。为了处理可变字符串,Java提供了StringBuffer和StringBuilder类。StringBuffer是线程安全的,适用于多线程环境下的字符串操作,而StringBuilder则是非线程安全的,适用于单线程环境下的字符串操作。这两个类提供了一系列的方法来对字符串进行连接、比较、截取、查找和替换等操作。\[1\]\[2\]在Java中,字符串可以通过使用+运算符或者调用String类的concat()方法来进行连接。此外,还可以使用String类的substring()方法来截取字符串的一部分,使用indexOf()方法来查找指定字符或子字符串的位置,使用replace()方法来替换字符串中的字符或子字符串。以上是Java中字符串的一些基本操作,还有更多的方法可以根据具体需求进行使用。 #### 引用[.reference_title] - *1* [【Java】常用数据结构之字符串](https://blog.csdn.net/weixin_40109345/article/details/121048060)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Java-数据结构-字符串专题一>](https://blog.csdn.net/weixin_45532984/article/details/125984603)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值