fork(3) download
  1. #include <iostream>
  2. #include <string>
  3. #include <regex>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. long long int equalMatches(string str, regex pattern)
  9. {
  10. smatch matches;
  11. string temp;
  12. size_t position=str.find('=',0);
  13. string lewa=str.substr(0,position);
  14. string prawa=str.substr(position+1,str.size()-position);
  15. long long int suma=0;
  16. while(regex_search(lewa, matches, pattern))
  17. {
  18. if (matches[2]=="")
  19. {
  20. suma+=1;
  21. }
  22. else
  23. {
  24. temp=matches[2];
  25. suma+=atoll(temp.c_str());
  26. }
  27. lewa=matches.suffix();
  28. }
  29. while(regex_search(prawa, matches, pattern))
  30. {
  31. if (matches[2]=="")
  32. {
  33. suma-=1;
  34. }
  35. else
  36. {
  37. temp=matches[2];
  38. suma-=atoll(temp.c_str());
  39. }
  40. prawa=matches.suffix();
  41. }
  42. return suma;
  43. }
  44.  
  45.  
  46. int main()
  47. {
  48. long long int n;
  49. long long int x;
  50. long long int L;
  51. cin>>n;
  52.  
  53. regex x_plus("(^|[+])(\\d*?)(x{1})");
  54. regex x_minus("(-)(\\d*?)(x{1})");
  55. regex L_plus("(^|[+])(\\d+)([^x\\d]|$)");
  56. regex L_minus("(-)(\\d+)([^x\\d]|$)");
  57. for (int i=0; i<n; i++)
  58. {
  59. string rownanie;
  60. cin>>rownanie;
  61.  
  62. x=equalMatches(rownanie,x_plus)-equalMatches(rownanie,x_minus);
  63. L=equalMatches(rownanie,L_plus)-equalMatches(rownanie,L_minus);
  64.  
  65. if (x==0)
  66. cout<<"NIE"<<endl;
  67. else
  68. cout<<-L/x<<endl;
  69. }
  70.  
  71. return 0;
  72. }
Success #stdin #stdout 0s 15344KB
stdin
7
-x=-x
x=x
-2+x=x
-2-x=-x
-2-x=-1-x
2-x=-4+2x
-2x+x-1=-4x+5
stdout
NIE
NIE
NIE
NIE
NIE
2
2