C++ 代码
//AAA->AA
//AABB->AAB 从左往右优先 eg:AABBCC->AABCC
#include<iostream>
#include<cstring>
using namespace std;
int T;
const int N=1000010;
char s[N];
int main()
{
cin>>T;
while(T--)
{
cin>>s;
int k=0;
for(int i=0;s[i];i++)//'\0'的ascii码是0
{
s[k++]=s[i];//整个循环过程中都有k<=i
//依次判断两个规则
if(k>=3&&s[k-3]==s[k-2]&&s[k-2]==s[k-1]) k--;
if(k>=4&&s[k-4]==s[k-3]&&s[k-2]==s[k-1]) k--;
}
s[k]='\0';
cout<<s<<'\n';
}
return 0;
}
为啥T了。。。
很神奇,看不出我的python代码哪里错了,大佬们能解释一下么
评论里没有办法缩进代码
n = int(input())
for i in range(n):
s = input()
new = ‘’
for j in range(len(s)):
if j>=2 and s[j] == s[j-1] and s[j] == s[j-2]:
continue
if j>=3 and s[j] == s[j-1] and s[j-2] == s[j-3]:
continue
new += s[j]
print(new)
因为你的if判断一直是对原字符串判断的,应该是每次对修改之后的进行判断。比如:AABBCC->AABCC,而按你的代码输出的却是AABC
多谢大佬,懂了