Advertisement
daksh_ddt

Slash the string-Setter

Nov 10th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. /* Come on Code on!!!!
  2. TEAM:     iitbhu_tesserect
  3. College:  IIT(BHU), Varanasi
  4. */
  5.  
  6. #include <vector>
  7. #include <list>
  8. #include <map>
  9. #include <set>
  10. #include <queue>
  11. #include <deque>
  12. #include <stack>
  13. #include <bitset>
  14. #include <algorithm>
  15. #include <functional>
  16. #include <numeric>
  17. #include <utility>
  18. #include <sstream>
  19. #include <iostream>
  20. #include <iomanip>
  21. #include <cstdio>
  22. #include <cmath>
  23. #include <cstdlib>
  24. #include <cstring>
  25. #include <queue>
  26. #include <ctime>
  27. #include <cassert>
  28. #include <climits>
  29. #include <limits>
  30.  
  31. using namespace std;
  32.  
  33. typedef vector <int> vi;
  34. typedef pair< int ,int > ii;
  35.  
  36. #define S(a) scanf("%d",&(a))
  37. #define P(a) printf("%d",(a))
  38. #define PS printf(" ")
  39. #define NL printf("\n")
  40. #define SL(a) scanf("%lld",&(a))
  41. #define PL(a) printf("%lld",(a))
  42. #define LL long long int
  43. #define FOR(I,A,B) for(int I= (A); I<(B); ++I)
  44. #define all(c) c.begin(), c.end()
  45. #define stop system("pause")
  46. #define pb push_back
  47. #define mp make_pair
  48. #define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
  49. #define INDEX(arr,ind)          (lower_bound(all(arr),ind)-arr.begin())
  50. #define ff first
  51. #define ss second
  52.  
  53. #define imax numeric_limits<int>::max()
  54. #define imin numeric_limits<int>::min()
  55. #define lmax numeric_limits<ll>::max()
  56. #define lmin numeric_limits<ll>::min()
  57.  
  58.  
  59. #define N 2000 //Max string length
  60.  
  61. int z[N];
  62. string s;
  63. int n;
  64. int L = 0, R = 0;
  65.  
  66.  
  67. void prec(){
  68.     n = s.size();
  69.     L = 0;
  70.     R = 0;
  71.     FOR(i,0,n+1)
  72.         z[i]=0;
  73. }
  74.  
  75. void zAlgo(){
  76.     for (int i = 1; i < n; i++) {
  77.       if (i > R) {
  78.         L = R = i;
  79.         while (R < n && s[R-L] == s[R]) R++;
  80.         z[i] = R-L; R--;
  81.       } else {
  82.         int k = i-L;
  83.         if (z[k] < R-i+1) z[i] = z[k];
  84.         else {
  85.           L = i;
  86.           while (R < n && s[R-L] == s[R]) R++;
  87.           z[i] = R-L; R--;
  88.         }
  89.       }
  90.     }
  91. }
  92.  
  93. int main(){
  94.     //freopen("input04.txt","r",stdin);
  95.     //freopen("output04.txt","w",stdout);
  96.     int t;
  97.     S(t);
  98.     while(t--){
  99.         cin>>s;
  100.         int ans = 0;
  101.         while(s.size()){
  102.             //cout<<s<<endl;
  103.             //cout<<s<<endl;  //remove this statement afterwards   NOTE: Specify that the middle string should not be prefix or suffix itself
  104.             prec();
  105.             zAlgo();
  106.             int mx = -1;
  107.             FOR(i,0,n)
  108.                 mx = max(mx,z[i]);
  109.             if(mx>0){
  110.                 //process the prefix part
  111.                 string temp = "";
  112.                 FOR(i,mx,n)
  113.                     temp+=s[i];
  114.                 s = temp;
  115.                 ans++;
  116.             }
  117.             else{                //process the suffix part
  118.                 reverse(s.begin(),s.end());
  119.                 prec();
  120.                 zAlgo();
  121.                 int zx = -1;
  122.                 FOR(i,0,n)
  123.                     zx = max(zx,z[i]);
  124.                 if(zx>0){
  125.                     //slash the suffix
  126.                     string temp = "";
  127.                     FOR(i,zx,n)
  128.                         temp+=s[i];
  129.                     s = temp;
  130.                     ans++;
  131.                     reverse(s.begin(),s.end());
  132.                 }
  133.                 else{
  134.                     //drop the last character
  135.                     reverse(s.begin(),s.end());
  136.                     string temp = "";
  137.                     FOR(i,1,n)
  138.                         temp+=s[i];
  139.                     s = temp;
  140.                     ans++;
  141.                    
  142.                 }
  143.             }
  144.             //cout<<s<<" "<<ans<<endl;
  145.            
  146.         }
  147.         P(ans);
  148.         NL;
  149.     }
  150.     //stop;
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement