hihocoder 字符消除

来源:互联网 发布:linux解压gz文件命令 编辑:程序博客网 时间:2024/06/05 10:57
 

C++中#include<string>与#include<string.h>的区别

1. 消除重字,熟悉string类操作

#include<iostream>
#include<string>
using namespace std;
string delestr(string s){
    string sc;
    for(int j=0;j<s.length()-1;j++){
        if(s[j]!=s[j+1]){
            if(j==0||s[j]!=s[j-1]){
                sc+=s[j];
            }
        }
    }
if(s[s.length()-2]!=s[s.length()-1]){
sc+=s[s.length()-1];
}
    if(sc!=s && sc.length()>1){
        sc=delestr(sc);
    }
    return sc;
}


int main(){
    int strnum;
int Max[100];
    int maxscore=0,score;
    cin>>strnum;
    for(int i=0;i<strnum;i++){
        string s,t,scopy;
cin>>s;
        //while(cin>>s){
            for(int k=0;k<s.length()+1;k++){
                for(int m=0;m<3;m++){
                    t=s.substr(0,k);
                    t+=('A'+m);
                    t+=s.substr(k,s.length()-k);
                    scopy=delestr(t);
                    score=s.length()-scopy.length()+1;
                    maxscore=(score>maxscore)?score:maxscore;
                               
             }   
                
            }
Max[i]=maxscore;
maxscore=0;
        //}
    }
for(int w=0;w<strnum;w++)
cout<<Max[w]<<endl;
system("pause");
    return 0;
}