标签:for test 处理 cto math str 字符 base cout
题目链接:https://vjudge.net/contest/344930#problem/E
题目大意:给出一个字符串,在末尾补充最少的字母,使其整个成为一个回文串
题目思路:对字符串进行顺序Hash和逆序Hash,然后去枚举位置,如果此时顺序的Hash和逆序的Hash值想等就说明此时是一个回文串。然后就直接输出该回文串前面的部分,再输出该回文串。如果没有回文串,那么就使整个字符串成为回文串
1 #include <stdio.h> 2 #include <algorithm> 3 #include <iostream> 4 #include <stdlib.h> 5 #include <string> 6 #include <string.h> 7 #include <math.h> 8 #include <vector> 9 #include <queue> 10 #include <stack> 11 #include <map> 12 #include <set> 13 14 15 #define INF 0x3f3f3f3f 16 #define LL long long 17 18 typedef unsigned long long ull; 19 const int maxn = 1e5+10; 20 21 char s[maxn]; 22 ull base = 131; 23 ull mod = 1e9+7; 24 ull p[maxn]; 25 ull h1[maxn],h2[maxn]; 26 ull q[maxn]; 27 28 29 ull get_hash(ull h[],int l,int r){ 30 return (h[r] - h[l-1]*p[r-l+1]); 31 } 32 33 // amanap lanacanal panama 34 35 int main() { 36 p[0] = 1; 37 for (int i=1;i<maxn;i++) { 38 p[i] = p[i-1] * base; 39 } 40 while (std::cin >> s+1) { 41 int len = strlen(s+1); 42 memset(h1,0, sizeof(h1)); 43 memset(h2,0, sizeof(h2)); 44 for (int i=1;i<=len;i++) { 45 h1[i] = h1[i-1] * base + s[i] - ‘a‘; 46 } 47 for (int i=len;i>=1;i--) { 48 h2[i] = h2[i+1] * base + s[i] - ‘a‘; 49 } 50 bool flag = false; 51 for (int i=1;i<=len;i++) { 52 if (get_hash(h1,i,len) == h2[i]) { 53 flag = true; 54 for (int k=1;k<=len;k++) { 55 std::cout << s[k]; 56 } 57 for (int k=i-1;k>=1;k--) { 58 std::cout << s[k]; 59 } 60 break; 61 } 62 } 63 if (!flag) { 64 for (int i=1;i<=len;i++) { 65 std::cout << s[i]; 66 } 67 for (int i=len-1;i>=1;i--) { 68 std::cout << s[i]; 69 } 70 } 71 std::cout << std::endl; 72 } 73 return 0; 74 }
Extend to Palindrome (顺序Hash和逆序Hash处理回文)
标签:for test 处理 cto math str 字符 base cout
原文地址:https://www.cnblogs.com/-Ackerman/p/11955026.html