LA_6439_PastiPas!

Regionals 2013 >> Asia - Jakarta

6439 - Pasti Pas!

Time limit: 2.000 seconds

Pertamina is an Indonesian government-owned oil and gas corporation based on Jakarta. It has by far
the largest distribution network of petroleum products compared to other oil companies in Indonesia.
When several foreign oil companies like Shell (Dutch) and Petronas (Malaysia) expanded their business
to Indonesia, Pertamina was faced with a great challenge to improve their service quality and strengthen
its consumer's trust, especially on their gas stations. At that time, Pertamina started a campaign
called \Pasti Pas!" to inform consumers that Pertamina will satisfy them by providing an accurate
measurement when they buy oil/gas at their gas stations. \Pasti Pas!" itself means \absolutely
accurate". Despite of what happened out there, we will focus our attention to the phrase \Pasti Pas!"
in this problem.
\Pasti Pas!" is an interesting phrase. Most of you must already know what a palindrome is,
i.e. a word or phrase which can be read the same way in either forward or backward direction, e.g.,
MADAM, AMOREROMA, etc. Of course \Pasti Pas!" is not a palindrome by this de nition; however,
if we replace one or more substring into another symbol, we will get a palindrome! For convenience,
let's remove all non alphabetical characters from the phrase. Let = `PAS' and = `TI', then the
phrase `PASTIPAS' will become ` ' which is a palindrome.
Now we are interested in what the palindrome value of a string S is. Palindrome value of a string
S is de ned as the length of the longest palindrome string S
′ where S

is derived from S by replacing
one or more substring by some symbols. As for the previous example, PASTIPAS has a palindrome
value of 3. Note that when deriving string S, each unique substring can only be mapped into a unique
symbol.
Here is another example. Let S = `ABCADDABCA'. There are several derivations of S, e.g.:
Let = `ABCA', = `DD', then S
′ = ` ' which has a length of 3.
Let = `ABCA', = `D', then S
′ = ` ' which has a length of 4.
Let = `A', = `BC', 
 = `D', then S
′ = `


' which has a length of 8.
Among all possible derivations of S, the longest palindrome can be formed has the length of 8.
Input
The rst line of input contains an integer T (T 100) denoting the number of cases. Each case contains
a string S (1 jSj 50;000; S 2 f`A' : : : `Z'g) in a line.
Output
For each case, output `Case #X: Y ', where X is the case number starts from 1 and Y is the palindrome value of S for each case.
Sample Input
4
PASTIPAS
ABCADDABCA
MADAMIAMADAM
ACMICPCJAKARTASITE
Sample Output
Case #1: 3
Case #2: 8
Case #3: 11
Case #4: 1


意思是本来不是回文串的可以把一些字母组合成块

这些块之间构成回文关系

看了别人写的才知道这是个字符串hash

但是自己的水方法居然也过了……

从两边往中间找,如果目前的块前后一致则可以加2

同时把块清空,否则继续往下扫

但是感觉如果数据出的刁钻点可能这样会t

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

const int M=5e4+5;
char s[M];
char s1[M];
char s2[M];

int main()
{
    int ans;
    int t;
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        scanf("%s",s+1);
        int len=strlen(s+1);
        int p=0;
        ans=0;
        for(int i=1;i<=len/2;i++)
        {
            s1[p]=s[i];
            s2[p++]=s[len+1-i];       //向前后块各添加一个字符
            //cout<<s1[p-1]<<" "<<s2[p-1]<<" ";
            int f=1;
            for(int j=0;j<p;j++)
            {
                //cout<<s1[p]<<" "<<s2[p-1]<<" ";
                 if(s1[j]!=s2[p-1-j])  //前后块不匹配
                {
                    f=0;
                    break;
                }
            }
            //cout<<f<<endl;
            if(f)                 //目前前后块匹配
            {
                p=0;
                ans+=2;
            }
        }
        if(p||len%2)        //串长为奇数时,或者中间存在不能匹配的段
            ans++;
        printf("Case #%d: %d\n",ca,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值