Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<set>
  5. #include<map>
  6. #include<string>
  7. #include<iostream>
  8. #include<cstring>
  9. #include<cctype>
  10. #include<queue>
  11. #include<list>
  12. #include<cstdlib>
  13. #include<cmath>
  14. #include<deque>
  15. using namespace std;
  16.  
  17. typedef long long LL;
  18. typedef pair<int,int> para;
  19. typedef vector<int> VI;
  20. typedef vector<vector<int> > VII;
  21. typedef vector<string> VS;
  22.  
  23. #define PB push_back
  24. #define MP make_pair
  25. #define F first
  26. #define S second
  27. #define FOREACH(a,n) for(__typeof(n.begin())a=(n).begin(); a!=(n).end(); ++a)
  28. #define REP(i,n) for(int i=0;i<(n);i++)
  29. #define FOR(i,a,b) for(int i=(a);i<=(b);i++)
  30. #define FUP(i,a,b) for(int i=(a);i<=(b);i++)
  31. #define FORD(i,a,b) for(int i=(a);i>=(b);i--)
  32. #define ALL(x) x.begin(),x.end()
  33. #define SIZE(x) ((int)x.size())
  34.  
  35. char tab[1000010];
  36. char st[1000000];
  37. int w = 0;
  38.  
  39. LL m;
  40. LL str_size;
  41. int stan = 0;
  42. LL dlug = 0;
  43.  
  44. bool chdl(LL ad){
  45.   dlug += ad;
  46.   return dlug <= m;
  47. }
  48.  
  49. void change_d(){
  50.   if(w > 0 && st[w-1] == 'd')
  51.     st[w-1] = 'D';
  52.   else if(w > 0 && st[w-1] == 'D')
  53.     st[w-1] = 'd';
  54. }
  55.  
  56. /*
  57.   STANY:
  58.  
  59.   0 - nic szczegolnego
  60.   1 - koniec wejscia
  61.  
  62.   3 - czekam na cyfre lub dwukropek w stringu
  63.   4 - czekam na dwukropek w stringu bo mialem wiodace zero
  64.   8 - czekam na znaki w stringu
  65.  
  66.   5 - czekam na cyferke w intcie bo bylo 'i'
  67.   6 - czekam na cyferke w intcie i juz byly jakies
  68.   7 - czekam na 'e' bo bylo wiodace zero w intcie
  69. */
  70.  
  71. void blad(int pos){
  72.   printf("Error at position %d!\n", pos);
  73. }
  74.  
  75. int main()
  76. {
  77.   scanf("%lld", &m);
  78.   scanf("%s", tab);
  79.  
  80.   int g = 0;
  81.   while(1){
  82.     char t = tab[g++];
  83.  
  84.     if(t == '\0'){ // koniec wejscia
  85.       if(stan == 1) printf("ok\n");
  86.       else blad(g-1); // za wczesnie
  87.       break;
  88.     }
  89.  
  90.     if(stan == 1){ // mial byc koniec
  91.       blad(g-1);
  92.       break;
  93.     }
  94.     else if(stan == 0){
  95.       if(t >= '0' && t <= '9'){ // zaczyna sie string
  96.         st[w++] = 's';
  97.         if(t == '0') stan = 4;
  98.         else stan = 3;
  99.  
  100.         str_size = t - '0';
  101.  
  102.         if(!chdl(1 + 1 + str_size)){ blad(g-1); break; }
  103.       }
  104.       else if(t == 'i'){
  105.         st[w++] = 'i';
  106.         stan = 5;
  107.  
  108.         if(!chdl(1 + 1 + 1)){ blad(g-1); break; }
  109.       }
  110.       else if(t == 'l'){
  111.         st[w++] = 'l';
  112.         stan = 0;
  113.        
  114.         if(!chdl(1 + 1)){ blad(g-1); break; }
  115.       }
  116.       else if(t == 'd'){
  117.         st[w++] = 'd'; // XXX: jak zaznaczam value/key ?
  118.         stan = 0;
  119.  
  120.         if(!chdl(1 + 1)){ blad(g-1); break; }
  121.       }
  122.       else if(t == 'e'){
  123.         if(w == 0){ blad(g-1); break; }
  124.         if(st[w-1] == 'l' || st[w-1] == 'd'){
  125.           w--;
  126.           if(!w) stan = 1; else stan = 0;
  127.         }
  128.         else if(st[w-1] == 'D'){
  129.           // blad - brakuje value bo byl key
  130.           blad(g-1);
  131.           break;
  132.         }
  133.       }
  134.       else{ blad(g-1); break; }
  135.     }
  136.     else if (stan == 3){
  137.       if(t >= '0' && t <= '9'){
  138.         if(!chdl(1LL + str_size * 9 + (t - '0'))){ blad(g-1); break; }
  139.         str_size = str_size * 10 + (t - '0');
  140.       }
  141.       else if(t == ':'){
  142.         stan = 8;
  143.       }
  144.       else{ blad(g-1); break; }
  145.     }
  146.     else if(stan == 4){
  147.       if(t == ':'){ // skonczyl sie string
  148.         w--;
  149.         if(!w) stan = 1; else stan = 0;
  150.         change_d();
  151.       }
  152.       else{ blad(g-1); break; }
  153.     }
  154.     else if(stan == 8){
  155.       if(--str_size == 0){
  156.         stan = 0; // skonczyl sie string
  157.         w--;
  158.         if(!w) stan = 1; else stan = 0;
  159.         change_d();
  160.       }
  161.     }
  162.     else if(stan == 5){
  163.       if(t == '0'){
  164.         stan = 7;
  165.       }
  166.       else if(t >= '1' && t <= '9'){
  167.         stan = 6;
  168.       }
  169.       else{ blad(g-1); break; }
  170.     }
  171.     else if(stan == 6){
  172.       if(t >= '0' && t <= '9'){
  173.         if(!chdl(1)){ blad(g-1); break; }
  174.       }
  175.       else if(t == 'e'){
  176.         w--;
  177.         if(!w) stan = 1; else stan = 0;
  178.         change_d();
  179.       }
  180.       else{ blad(g-1); break; }
  181.     }
  182.     else if(stan == 7){
  183.       if(t == 'e'){
  184.         w--;
  185.         if(!w) stan = 1; else stan = 0;
  186.         change_d();
  187.       }
  188.       else{ blad(g-1); break; }
  189.     }
  190.   }
  191.  
  192.   return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement