新聞| | PChome| 登入
2012-02-22 08:33:08| 人氣3,559| 回應13 | 上一篇 | 下一篇

[UVA] 11475 - Extend to Palindrome

推薦 0 收藏 0 轉貼0 訂閱站台

Problem E

Extend to Palindromes

Time Limit : 3 seconds

 

 

 

Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse it) of length at least N. Any palindrome will do. Easy, isn't it? That's what you thought before you passed it on to your inexperienced team-mate. When the contest is almost over, you find out that that problem still isn't solved. The problem with the code is that the strings generated are often not palindromic. There's not enough time to start again from scratch or to debug his messy code. Seeing that the situation is desperate, you decide to simply write some additional code that takes the output and adds just enough extra characters to it to make it a palindrome and hope for the best. Your solution should take as its input a string and produce the smallest palindrome that can be formed by adding zero or more characters at its end.

 

 

 

Input

 

 

 

 

Input will consist of several lines ending in EOF. Each line will contain a non-empty string made up of upper case and lower case English letters ('A'-'Z' and 'a'-'z'). The length of the string will be less than or equal to 100,000.

 

 

 

 

 

 

 

Output

 

 

 

For each line of input, output will consist of exactly one line. It should contain the palindrome formed by adding the fewest number of extra letters to the end of the corresponding input string.

 

 

 

 

 

 

 

Sample Input

Sample Output

 

 

 

 

 

aaaa
abba
amanaplanacanal
xyz

aaaa
abba
amanaplanacanalpanama
xyzyx








做法 : KMP

做出另外一個反轉字串的字串, 與原先的字串做KMP匹配

#include<stdio.h>
#include<string.h>
#define MaxL 100005
char A[MaxL], B[MaxL];
int P[MaxL];
void KMPTable(char *A) {
    int i, j;
    P[0] = -1, i = 1, j = -1;
    while(A[i]) {
        while(j >= 0 && A[j+1] != A[i])
            j = P[j];
        if(A[j+1] == A[i])
            j++;
        P[i++] = j;
    }
}
int KMPMatching(char A[], char B[]) {
    KMPTable(B);
    int i, j, Alen, Blen;
    Alen = strlen(A), Blen = strlen(B);
    for(i = 0, j = -1; i < Alen; i++) {
        while(j >= 0 && B[j+1] != A[i])
            j = P[j];
        if(B[j+1] == A[i])    j++;
/*        if(j == Blen-1)
            return true;
*/
    }
    return j;
}
void Solve(char A[]) {
   
    int Alen = strlen(A), Blen = Alen;
    int i, j;
    for(i = 0, j = Blen-1; i < Alen; i++, j--)
        B[j] = A[i];
    B[Blen] = '\0';
    int tail = KMPMatching(A, B);
    for(i = Blen-1; i > tail; i--)
        printf("%c", B[i]);
    printf("%s\n", B);
}
int main() {
    while(scanf("%s", A) == 1)
        Solve(A);
    return 0;
}

台長: Morris
人氣(3,559) | 回應(13)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 11888 - Abnormal 89's
此分類上一篇:[UVA] 10298 - Power Strings

srp
The software says stunning to read these sort of revealing and additionally completely unique articles or reviews upon your webpages. infused thca flower http://www.oregonobserver.com/discover-the-best-thca-flower-a-2024-guide/article_79d958c8-75f7-11ef-9b27-5355d363a41c.html
2025-05-27 19:30:16
seo
Many thanks with regard to supplying current improvements concerning the issue, We anticipate study much more. plus size bachelorette underwear http://www.amazon.com/NE-Beauty-Halloween-Medium-Orange/dp/B0F3NQYDZF/
2025-06-24 19:43:05
sei
Excellent Submit, I will be a huge believer inside submitting feedback about web sites to be able to allow the website copy writers understand that they’ve extra one thing useful to be able to the net! polkadot chocolate http://meridian-puff.com/product-category/polkadot-chocolate/
2025-06-25 18:35:01
seo
Seriously sturdy, magnificent, fact-filled information and facts listed here. A person's discussions Never need disappoint, and the unquestionably is valid listed here in addition. You actually continually make a fun learn. Do you convey to I'm just happy?: )#) Keep up to date the nice reports. http://ptbola.net/ https://ptbola.net/
2025-06-26 21:00:22
seo
You might be allowed to submit brands, however, not back links, except if they may be accepted and also about matter. promo code for 1xbet india https://workfromhome24h.com/pag/1xbet-promo-code-tanzania_2.html
2025-06-29 21:05:38
JimmySEO
Fantastic write-up, journeyed onward in addition to book-marked your web site. When i can’t hang on to learn to read far more by people melbet promo code 2026. https://copychief.com/art/melbet_promo_codes.html
2025-06-29 22:49:32
Jimmy
You might be allowed to submit brands, however, not back links, except if they may be accepted and also about matter.melbet promo code bd http://fineart.sk/cache/permission/best_promo_code_for_melbet_registration_2.html
2025-06-29 22:51:38
seo
Superb blog post. Any place strikes numerous pressing obstacles of your modern culture. People can not be uninvolved that will those obstacles. The place delivers guidelines together with thoughts. Rather interesting together with handy. http://jetx2game.com https://jetx2game.com
2025-07-03 23:16:00
seo
May possibly fairly recently up and running an important web log, the data one offer you on this web site contains given a hand to all of us substantially. Bless you designed for your current precious time & get the job done. haha178 Daftar http://gaudena.com/
2025-07-05 21:20:39
dsfdsfd
This seems amazing to see this kind of educational as well as distinctive content articles in your web sites.IPTV UK https://xtremehd-uk.uk/
2025-07-07 19:48:38
sdasd
IPTV Smarters Pro payant en France offre un service de streaming TV de haute qualité avec un large choix de chaînes, films et séries. Cet abonnement premium garantit une diffusion fluide et sans interruption, avec une résolution HD ou supérieure. Compatible avec plusieurs appareils comme smartphones, Smart TV et PC, il propose des fonctionnalités avancées telles que le guide TV (EPG), le contrôle parental et la gestion multi-écrans. Idéal pour les utilisateurs recherchant un divertissement complet, fiable et sécurisé en France IPTV Smarters Pro payant France http://officieliptvsmarters.com/
2025-07-09 21:12:39
sdfsf
It is a fantastic post – immense clear and easy to understand. I am also holding out for the sharks too that made me laugh. situs toto http://spiritualedge.org/contact
2025-07-14 16:58:10
dsfdf
Friend, this web site might be fabolous, i just like it. olabahis giriş http://olabahis.com.de/
2025-07-29 15:54:31
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文