USACO 1.3 Calf Flac (calfflac)

/*
ID: haolink1
PROG: calfflac
LANG: C++
*/

//#include <iostream>
#include <fstream>
#include <string>
using namespace std;

typedef unsigned int u_int;

bool IsEqual(char first,char second){
    if(first == second || first + 32 == second || first == second +32)
        return true;
    else 
        return false;
}

bool IsValid(char target){
    if((target >= 65 && target <= 90) || (target >= 97 && target <= 122))
        return true;
    else 
        return false;
}
int main(){
    ifstream fin("calfflac.in");
    char total_string[20000];
    //read all the file data form stream into char array;
    fin.read(total_string,20000);
    int pal_begin = 0;
    int pal_end = 0;
    int max_pal_len = 1;
    int total_len = fin.gcount();
    // found odd palindromes and the core character of the palindromes must be valid,
    // begin with position 1
    for(int i = 1; i < total_len-1; i++){
        if(!IsValid(total_string[i]))// the core character of the palindromes must be valid, 
            continue;
        int temp_pal_len = 1;
        int forward = i-1;
        int backward = i+1;
        while(true){
            while(forward >= 0){
                if(IsValid(total_string[forward]))
                    break;
                else
                    forward--;
            }
            while(backward < total_len){
                if(IsValid(total_string[backward]))
                    break;
                else
                    backward++;
            }
            if(forward < 0 || backward >= total_len)
                break;
            if(IsEqual(total_string[forward],total_string[backward])){
                temp_pal_len+=2;
                if(temp_pal_len > max_pal_len){
                    pal_begin = forward;
                    pal_end = backward;
                    max_pal_len = temp_pal_len;
                }
                //Note to update the below two variable 
                forward--;
                backward++;
            }else{
                break;
            }
        }
    }        
    //find even palindromes 
    for(int i = 1; i < total_len-1; i++){
        if(!IsValid(total_string[i]))// the core character of the palindromes must be valid,
            continue;
        int temp_pal_len = 0;
        int forward = i;
        int backward = i+1;
        while(true){
            while(forward >= 0){
                if(IsValid(total_string[forward]))
                    break;
                else
                    forward--;
            }
            while(backward < total_len){
                if(IsValid(total_string[backward]))
                    break;
                else
                    backward++;
            }
            if(forward < 0 || backward >= total_len)
                break;
            if(IsEqual(total_string[forward],total_string[backward])){
                temp_pal_len+=2;
                if(temp_pal_len > max_pal_len){
                    pal_begin = forward;
                    pal_end = backward;
                    max_pal_len = temp_pal_len;
                }
                forward--;
                backward++;
            }else{
                break;
            }
        }
    }
    ofstream fout("calfflac.out");
    fout<< max_pal_len <<endl;
    string result(total_string+pal_begin,pal_end - pal_begin +1);
    fout << result << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值