不知道这题为什么那么少解题报告?好吧,我就为各大搜索引擎新添一抹风景好了…… /* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2011 panyanyany All rights reserved. URL : http://acm.hdu.edu.cn/showproblem.php?pid=1867 Name : 1867 A + B for you again Date : Monday, May 9, 2011 Time Stage : 2 hours around Result: 3929770 2011-05-09 01:49:26 Accepted 1867 31MS 812K 1611 B C++ pyy Test Data: Review: 这题其实不太难,就是长一点而已。 为了节约时间,可以不连接字符串,直接截取输出。 //----------------------------------------------------------------------------*/ #include <iostream> #include <string.h> #include <stdio.h> using namespace std; const int size = 200010; char str1[size], str2[size], dest[size]; int next[size]; void getNext( const char *str ) { int i, j, k, len = strlen(str); i = 0; j = 1; next[0] = -1; while( j < len ) { if( str[i] == str[j] ) { next[j] = next[i]; ++i; } else { next[j] = i; } ++j; } } int kmp( const char *master, const char *slave ) { int i, j, k; const int leng = (int)strlen(master), len = (int)strlen(slave); i = j = 0; getNext( slave ); while( i < leng && j < len ) { if( master[i] == slave[j] || -1 == j ) { ++i; ++j; } else { j = next[j]; } } if( leng == i ) { return j; } else return 0; } int main() { int i, j, k; while( scanf("%s%s", str1, str2) != EOF ) { const int res1 = kmp(str1, str2), res2 = kmp(str2, str1); if( res1 == res2 ) { if( strcmp(str1, str2) < 0 ) { printf("%s%s/n", str1, str2+res1); } else { printf("%s%s/n", str2, str1+res1); } } else if( res1 < res2 ) { printf("%s%s/n", str2, str1+res2); } else { printf("%s%s/n", str1, str2+res1); } } return 0; }