import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); String s=sc.next(); int count=1; for(int i=1;i<s.length();i++){ if(s.charAt(i)==s.charAt(i-1)){ count++; if(i==s.length()-1&&count>=3){ s=s.substring(0,i-2); count=0; i=0; } continue; } if(count>=3){ //很奇怪不是减count个,而是-3个,估计是题目的问题吧。。。 s=s.substring(0,i-3)+s.substring(i); i=0; } count=1; } System.out.println(s); } }
let s = readline() let cnt = 1 let end = 0, start = 0 for (let i = 1; i < s.length; i++) { if (s.charAt(i) === s.charAt(i - 1)) { cnt++ end = i start = end - cnt + 1 } else { start = i cnt = 1 } // console.log(s.charAt(i), cnt, start, end) 如果这里直接用if判断,那么cnt一到三就会进入这个判断,所以结果是只删除了三个 如果要删除三个以上,这里的分支要用 else if,并且去掉上面的else分支 if (cnt >= 3) { let s1 = s.slice(0, start) let s2 = s.slice(end + 1, s.length) s = s1.concat(s2) // console.log('s1:', s1) // console.log('s2:', s2) // console.log('s3:', s) cnt = 1 i = 0 } } // } console.log(s)
# 如果按题目要求的话应该得到 “A”:
def deletetriple(text : str):
triples = 1
while len(text)>=3 and triples>0:
num = 0
triples = 0
for x in range(len(text)-1):
if text[x] == text[x+1]:
num+=1
else:
if num >=2:
triples += 1
text = "".join([text[:x-num], text[x+1:]])
break
else:
num = 0
return text
# 如果要得到“ABA”答案的话:
def deletetriple(text : str):
triples = 1
while len(text)>=3 and triples>0:
num = 0
triples = 0
for x in range(len(text)-1):
if text[x] == text[x+1]:
num+=1
else:
if num >=2:
triples += 1
text = "".join([text[:x-num], text[x-num+3:]]) # 只去掉连续三个char
break
else:
num = 0
return text
print(deletetriple("AAABBBAAABBBAAAABBBAABBBAAAABBBBA")
另外我发现这系统好坑,我用得出“A”的方式,它就给“ABA”的结果,我用得出“ABA”的方式,它就给我“A”的答案。。。。。。。真的好无语。。。。。。(有截图图但是死活传不上。。。这系统太折磨人了) #include<iostream> #include<string> using namespace std; int main() { string str; cin>>str; int len=str.length(); for(int i=0;i<len;) { int flag=0,l=3; if(str[i+1]==str[i]) { if(str[i+2]==str[i]) { flag=1; for(int j=i+3;str[j]==str[i];j++) { l++; } } } if(flag) { str.erase(i,l); len=len-l; if(i>=2) { i=i-2; continue; } } else { i++; } } cout<<str<<endl; }
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder str = new StringBuilder(br.readLine()); boolean flg = true; int i = 0,j; while(flg){ flg = false; for (i = 0,j = 2; j < str.length(); ) { if (str.charAt(j)==str.charAt(i)&&str.charAt(i)==str.charAt(i+1)){ j++; str.delete(i,j); flg = true; break; }else{ i++; j = i+2; } } } System.out.println(str); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author wylu */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); boolean flag = true; while (flag) { flag = false; for (int i = 0, j = 1; j < s.length();) { while (j < s.length() && s.charAt(j) == s.charAt(i)) j++; if (j - i >= 3) { s = s.substring(0, i) + s.substring(i + 3); flag = true; break; } else { i = j; j = i + 1; } } } System.out.println(s); } }