Link: Ultraviolet A 11475-extend to palindrome
Given a string, the minimum number of characters to be added to the output makes the string a return string.
Solution: use string transpose as KMP, then use the original string to match, and finally the matching length is the repeated length.
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1e5+5;char s[maxn], t[maxn];int n, jump[maxn];void get_jump () { int p = 0; for (int i = 2; i <= n; i++) { while (p && s[p + 1] != s[i]) p = jump[p]; if (s[p + 1] == s[i]) p++; jump[i] = p; }}int find () { int p = 0; for (int i = 1; i <= n; i++) { while (p && s[p + 1] != t[i]) p = jump[p]; if (s[p + 1] == t[i]) p++; } return p;}int main () { while (scanf("%s", s + 1) == 1) { printf("%s", s+1); n = strlen(s + 1); for (int i = 1; i <= n + 1; i++) t[i] = s[i]; reverse(s + 1, s + n + 1); get_jump(); int k = find(); for (int i = k + 1; i <= n; i++) printf("%c", s[i]); printf("\n"); } return 0;}
Ultraviolet A 11475-extend to palindrome (KMP)