题意: 向给定的字符串中插入一个字符(A、B 或 C),然后消除左右相同的字符,问插入某一个位置后最多能够消除多少个字符。
思路: 字符串长度不超过 100,T 不超过 100,数据比较小,暴力枚举一下插入的位置也不会超时。所以思路就是暴力枚举每一个插入的位置,然后求出能够消除的字符个数,取最大的即是最终结果。
代码:
还是那个问题,不同的思路实现起来一点都不一样,有的都不知道怎么实现,还容易出错,有的就简单易懂,所以找到一个好思路很重要。
比如,这道题消除字符过程中,本来想的是记录上一个字符然后和当前的字符比较看是否相同,实现起来很烦。。还 WA 了。后来直接判断当前字符和左右字符有没有相同的,实现起来就很简单了。
下面的程序消除字符用的是迭代。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<utility>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;
typedef long long LL;
int T;
char str[110];
char str1[110];
char str2[110];
int f()
{
bool ok = true;
int sum = strlen(str1);
while(ok){
ok = false; //用 ok 记录当前这个循环是否还有被消去的字符,如果没有则没有必要再进行循环了
int len = strlen(str1);
int len2 = 0;
if(len <= 1){
continue;
}
else if(str1[0] != str1[1]){
str2[len2++] = str1[0];
}
else if(str1[0] == str1[1]){
ok = true;
}
for(int i=1; i<len-1; i++){
if(str1[i]==str1[i-1] || str1[i]==str1[i+1]){
ok = true;
}
else{
str2[len2++] = str1[i];
}
}
if(str1[len-1] != str1[len-2]){
str2[len2++] = str1[len-1];
}
else{
ok = true;
}
str2[len2] = '\0';
strcpy(str1, str2);
}
return sum - strlen(str1);
}
int main()
{
//freopen("in.txt", "r", stdin);
while(scanf("%d", &T) == 1){
while(T--){
scanf("%s", str);
int sum = strlen(str);
int ans = 0;
int tmpans;
str1[0] = 'A';
for(int i=0; i<=sum; i++){
str1[i+1] = str[i];
}
tmpans = f();
ans = max(ans, tmpans);
str1[0] = 'B';
for(int i=0; i<=sum; i++){
str1[i+1] = str[i];
}
tmpans = f();
ans = max(ans, tmpans);
str1[0] = 'C';
for(int i=0; i<=sum; i++){
str1[i+1] = str[i];
}
tmpans = f();
ans = max(ans, tmpans);
for(int i=0; i<sum; i++){
int t = 0;
for(int j=0; j<=i; j++){
str1[t++] = str[j];
}
str1[t++] = 'A';
for(int j=i+1; j<sum; j++){
str1[t++] = str[j];
}
tmpans = f();
ans = max(ans, tmpans);
t = 0;
for(int j=0; j<=i; j++){
str1[t++] = str[j];
}
str1[t++] = 'B';
for(int j=i+1; j<sum; j++){
str1[t++] = str[j];
}
tmpans = f();
ans = max(ans, tmpans);
t = 0;
for(int j=0; j<=i; j++){
str1[t++] = str[j];
}
str1[t++] = 'C';
for(int j=i+1; j<sum; j++){
str1[t++] = str[j];
}
tmpans = f();
ans = max(ans, tmpans);
}
printf("%d\n", ans);
}
}
return 0;
}
看了看别人用 string 写的,string 的 insert 函数用起来太方便了。。。
下面的程序消除字符用的是递归。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<utility>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;
typedef long long LL;
int T;
int Cal(string src)
{
string dst = "";
int flag = false;
int len = src.size();
if(len <= 1){
return 0;
}
else if(src[0] != src[1]){
dst += src[0];
}
else if(src[0] == src[1]){
flag = true;
}
for(int i=1; i<len-1; i++){
if(src[i]==src[i-1] || src[i]==src[i+1]){
flag = true;
}
else{
dst += src[i];
}
}
if(src[len-1] != src[len-2]){
dst += src[len-1];
}
else{
flag = true;
}
//cout << dst << endl;
if(flag) return len - dst.size() + Cal(dst);
else return len - dst.size();
}
int main()
{
//freopen("in.txt", "r", stdin);
while(scanf("%d", &T) == 1){
string src, dst;
while(T--){
cin >> src;
int len = src.size();
int ans = 0;
for(int i=0; i<=len; i++){
dst = src;
dst.insert(i, "A");
ans = max(ans, Cal(dst));
dst = src;
dst.insert(i, "B");
ans = max(ans, Cal(dst));
dst = src;
dst.insert(i, "C");
ans = max(ans, Cal(dst));
}
printf("%d\n", ans);
}
}
return 0;
}

本文介绍了一种通过插入字符并消除相邻重复字符来优化字符串的操作方法。通过两种不同实现方式——迭代与递归,详细展示了如何高效地解决该问题,并对比了使用C风格字符串与C++标准库string的优势。
3万+

被折叠的 条评论
为什么被折叠?



