fork(3) download
  1. #include <stdio.h>
  2.  
  3. char str[50010];
  4.  
  5. unsigned long long x, y, P[50010], B = 1947698881ULL;
  6.  
  7. int F(int i, int j){
  8. if(i >= j) return (i == j);
  9.  
  10. x = y = 0;
  11. int r = 1, l = 0;
  12. while (i < j){
  13. x = x * B + str[i];
  14. y = P[l] * str[j] + y;
  15. if (x == y){
  16. r = 2 + F(i + 1, j - 1);
  17. break;
  18. }
  19. i++, l++, j--;
  20. }
  21. return r;
  22. }
  23. int main(){
  24. int T = 0, t, i;
  25. P[0] = 1ULL;
  26. for(i = 1; i < 50010; i++) P[i] = (P[i - 1] * B);
  27.  
  28. scanf("%d", &t);
  29. while (t--){
  30. scanf("%s", str);
  31. printf("Case #%d: %d\n", ++T, F(0, strlen(str) - 1));
  32. }
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 2732KB
stdin
4
PASTIPAS
ABCADDABCA
MADAMIAMADAM
ACMICPCJAKARTASITE
stdout
Case #1: 3
Case #2: 8
Case #3: 11
Case #4: 1