Calf Flac

本文介绍了一个名为CalfFlac的程序,用于从给定的文本中找出最长的回文串,忽略数字、标点和空白字符,但保留这些字符在最终输出中。程序首先剥离了文本中的非字母字符,然后寻找最大回文,并映射回原始文本。
摘要由CSDN通过智能技术生成
Calf Flac

It is said that if you give an infinite number of cows an infinite number of heavy-duty laptops (with very large keys), that they will ultimately produce all the world's great palindromes. Your job will be to detect these bovine beauties.

Ignore punctuation, whitespace, numbers, and case when testing for palindromes, but keep these extra characters around so that you can print them out as the answer; just consider the letters `A-Z' and `a-z'.

Find the largest palindrome in a string no more than 20,000 characters long. The largest palindrome is guaranteed to be at most 2,000 characters long before whitespace and punctuation are removed.

PROGRAM NAME: calfflac

INPUT FORMAT

A file with no more than 20,000 characters. The file has one or more lines which, when taken together, represent one long string. No line is longer than 80 characters (not counting the newline at the end).


SAMPLE INPUT (file calfflac.in)

Confucius say: Madam, I'm Adam.

OUTPUT FORMAT

The first line of the output should be the length of the longest palindrome found. The next line or lines should be the actual text of the palindrome (without any surrounding white space or punctuation but with all other characters) printed on a line (or more than one line if newlines are included in the palindromic text). If there are multiple palindromes of longest length, output the one that appears first.

SAMPLE OUTPUT (file calfflac.out)

11
Madam, I'm Adam
代码:
/*
 ID: jszhais1
 PROG: calfflac
 LANG: C++
 */

//#include <iostream>
#include <fstream>
using namespace std;
ifstream fin  ( "calfflac.in"  ) ;
ofstream fout ( "calfflac.out" ) ;
int main(int argc, const char * argv[])
{
    
    char transChar(char &ch_a);
    int maxPalindromes(char *ch,int &start,int &end,const int &chLength);
    char ch_a[20010];
    int ch_length=0;
    for(;!fin.eof();ch_length++)
      ch_a[ch_length]=fin.get();
    //for(;(ch_a[ch_length]=getchar())!='$';ch_length++);
    
    ch_a[ch_length]='\0';
    int start=0,end=0;
    int length = maxPalindromes(ch_a,start,end,ch_length-1);
    // cout<<"start: "<<start<<"  end: "<<end<<endl;
    fout<<length<<endl;
    for(int i=start;i<=end;i++)
        fout<<ch_a[i];
    fout<<endl;
    return 0;
}

int maxPalindromes(char *ch,int &start,int &end,const int &chLength)
{
    char transChar(char &ch_a);
    int MAX=0;
    for(int i=1;i<=chLength;i++)
    {
        int left = 0,right = 1,max=0;
        for(left=i,right=i;left>=0&&right<chLength;left--,right++)
        {
            //cout<<"singleRight"<<endl;
            while(left>=0&&transChar(ch[left])==-1)
                left--;
            while(right<chLength&&transChar(ch[right])==-1)
                right++;
            //max++;
            if(!(left>=0&&right<chLength)||transChar(ch[left])!=transChar(ch[right]))
            {
				//if(transChar(ch[right-1])==-1)
					right--;
				//if(transChar(ch[left+1])==-1)
					left++;
                break;
            }
            
        }//for
        for(int j = left;j<=right;j++)
			if(transChar(ch[j])!=-1)
				max++;
        if(max>MAX)
        {
            //cout<<"max1: "<<max<<"left: "<<left<<"right: "<<right<<endl;
            MAX = max;
            start = left;
            end = right;
        }//if
        
        
        if(transChar(ch[i])==transChar(ch[i+1]))
        {
            left = 0,right = 0,max=0;
            for(left=i,right=i+1;left>=0&&right<=chLength;left--,right++)
            {
                //cout<<"Hi: left: "<<left<<"  "<<"right: "<<right<<endl;
                while(left>=0&&transChar(ch[left])==-1)
                    left--;
                while(right<chLength&&transChar(ch[right])==-1)
                    right++;
                // max++;
                if(!(left>=0&&right<chLength)||transChar(ch[left])!=transChar(ch[right]))
                {
					//if(transChar(ch[right-1])==-1)
						right--;
					//if(transChar(ch[left+1])==-1)
						left++;
                    break;
                }
                
            }//for
			for(int j = left;j<=right;j++)
				if(transChar(ch[j])!=-1)
					max++;
			//max = right-left;
            if(max>MAX)
            {
                //cout<<"max2: "<<max<<"left: "<<left<<"right: "<<right<<endl;
                MAX = max;
                start = left;
                end = right;
            }//if
            
        }//if
        
    }//for
    //cout<<"["<<ch[end]<<"]"<<endl;
    
    while (transChar(ch[start])==-1) {
        start++;
    }
    while(transChar(ch[end])==-1)
        end--;
    int length=0;
    for(int i=start;i<=end;i++)
    {
        if(transChar(ch[i])!=-1)
            length++;
    }
    return length;
}

char transChar(char &ch_a)
{
    char ch_b;
    if(ch_a>='a'&&ch_a<='z')
        ch_b = ch_a;
    else if(ch_a>='A'&&ch_a<='Z')
        ch_b = ch_a+32;
    else
        ch_b=-1;
    return ch_b;
}

结果:
USER: jim zhai [jszhais1]
TASK: calfflac
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3360 KB]
   Test 2: TEST OK [0.000 secs, 3360 KB]
   Test 3: TEST OK [0.000 secs, 3360 KB]
   Test 4: TEST OK [0.000 secs, 3360 KB]
   Test 5: TEST OK [0.000 secs, 3360 KB]
   Test 6: TEST OK [0.000 secs, 3360 KB]
   Test 7: TEST OK [0.000 secs, 3360 KB]
   Test 8: TEST OK [0.313 secs, 3360 KB]

All tests OK.
答案:
Calf Flac
Russ Cox

To make the programming easier, we keep two copies of the text: the original, and one with the punctuation stripped out. We find the biggest palindrome in the latter copy, and then figure out which part of the original it corresponds to.

To find the biggest palindrome in the alphabetic text, we look, for each letter in the text, at the biggest palindrome centered on that letter (in the case of an odd length palindrome) or just to the right of that letter (in the case of an even length palindrome).

There are 20,000 letters, and we are assured that no palindrome is more than 2,000 letters long, and we search for both even and odd palindromes, for a total of about 20,000*2,000*2 = 80,000,000 operations, which is perfectly reasonable within the time limit.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>

char fulltext[21000];
char text[21000];

char *pal;
int pallen;

void
findpal(void)
{
    char *p, *fwd, *bkwd, *etext;
    int len;

    etext = text+strlen(text);
    for(p=text; *p; p++) {
	/* try palindrome with *p as center character */
	for(fwd=bkwd=p; bkwd >= text && fwd < etext && *fwd == *bkwd;
				fwd++, bkwd--)
			;
	bkwd++;
	len = fwd - bkwd;
	if(len > pallen) {
	    pal = bkwd;
	    pallen = len;
	}

	/* try palindrome with *p as left middle character */
	for(bkwd=p, fwd=p+1;
	          bkwd >= text && fwd < etext && *fwd == *bkwd; fwd++, bkwd--)
			;
	bkwd++;
	len = fwd - bkwd;
	if(len > pallen) {
	    pal = bkwd;
	    pallen = len;
	}
    }
}

void
main(void)
{
    FILE *fin, *fout;
    char *p, *q;
    int c, i, n;

    fin = fopen("calfflac.in", "r");
    fout = fopen("calfflac.out", "w");
    assert(fin != NULL && fout != NULL);

    /* fill fulltext with input, text with just the letters */
    p=fulltext;
    q=text;
    while((c = getc(fin)) != EOF) {
	if(isalpha(c))
	    *q++ = tolower(c);
	*p++ = c;
    }
    *p = '\0';
    *q = '\0';

    findpal();

    fprintf(fout, "%d\n", pallen);

    /* find the string we found in the original text
       by finding the nth character */
	n = pal - text;
    for(i=0, p=fulltext; *p; p++)
	if(isalpha(*p))
	    if(i++ == n)
		break;
    assert(*p != '\0');

    /* print out the next pallen characters */
    for(i=0; i<pallen && *p; p++) {
	fputc(*p, fout);
	if(isalpha(*p))
	    i++;
    }
    fprintf(fout, "\n");

    exit(0);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值