fork download
  1. #include<iostream>
  2. #include <string>
  3. #include <stdexcept>
  4.  
  5. template<class T>
  6. class GhostMemory{
  7. T* Memory;
  8. std::size_t N;
  9. public:
  10.  
  11. GhostMemory(T* P = nullptr, std::size_t L = 0): Memory(P), N(L) {}
  12. std::size_t Size(){ return N; }
  13. T& operator [](std::size_t Idx){
  14. if (Idx >= N) throw std::out_of_range("Out Of Range in GhostMemory::Operator[] !!");
  15. return Memory[Idx];
  16. }
  17. bool Reset(T* P=nullptr, std::size_t L=0){
  18. Memory = P;
  19. N = L;
  20. return true;
  21. }
  22. T* Pointer(){
  23. return Memory;
  24. }
  25. T* begin(){
  26. return Memory;
  27. }
  28. T* end(){
  29. return Memory + N;
  30. }
  31. T& Back(){
  32. return Memory[N - 1];
  33. }
  34. };
  35.  
  36. template<class T,std::size_t N>
  37. std::size_t CountOf(T(&A)[N]){
  38. return N;
  39. }
  40. std::string ToString(GhostMemory<char>& G){
  41. std::string R;
  42.  
  43. for (auto& o : G) R += o;
  44.  
  45.  
  46. return R;
  47. }
  48. std::string MakeHoge(char str[], std::size_t Len){
  49. std::size_t S = 0;
  50. GhostMemory<char> G;
  51. std::string R;
  52. bool F = true;
  53. for (S= 1; S < Len; S++)
  54. {
  55. if (str[S] == str[0])break;
  56. }
  57. G.Reset(str, S);
  58. for (std::size_t i = 0; i < Len - G.Size(); i++){
  59. if (G[i%G.Size()] != str[i]){
  60. S = i;
  61. }
  62. G.Reset(str, S);
  63. }
  64.  
  65. if (G.Back() != str[Len - 1])S = Len;
  66. G.Reset(str, S);
  67. R = ToString(G);
  68. return R;
  69.  
  70. }
  71.  
  72. bool Show(char* str,std::string R){
  73. std::cout << str << " -> " << R << std::endl;
  74. return true;
  75. }
  76.  
  77. int main(){
  78. char A[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  79. char B[] = "123412312341231234123123412312341231234123";
  80. char C[] = "oxoxoxoxoxoxoxoxxoxoxoxoxoxoxoxoxx";
  81. char D[] = "abac";
  82. char E[] = "axaxa";
  83. std::string R;
  84. R = MakeHoge(A,CountOf(A)-1);
  85. Show(A, R);
  86. R = MakeHoge(B,CountOf(B)-1);
  87. Show(B, R);
  88. R = MakeHoge(C,CountOf(C)-1);
  89. Show(C, R);
  90. R = MakeHoge(D,CountOf(D)-1);
  91. Show(D, R);
  92. R = MakeHoge(E,CountOf(E)-1);
  93. Show(E, R);
  94. return 0;
  95. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -> a
123412312341231234123123412312341231234123 -> 1234123
oxoxoxoxoxoxoxoxxoxoxoxoxoxoxoxoxx -> oxoxoxoxoxoxoxoxx
abac -> abac
axaxa -> axaxa