fork(1) download
  1. // https://p...content-available-to-author-only...j.com/problems/FR_07_03/
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <cstdio>
  7. #include <cctype>
  8. #include <cstring>
  9. #include <bitset>
  10. #include <complex>
  11. #include <queue>
  12. #include <deque>
  13. #include <map>
  14. #include <stack>
  15. #include <iterator>
  16. #include <list>
  17. #include <set>
  18.  
  19. using namespace std;
  20.  
  21. typedef vector<unsigned int> VI;
  22. typedef long long LL;
  23. #define FOR(x, from, to) for(int x = from; x <= (to); ++x)
  24. #define FORD(x, from, to) for(int x = from; x >= (to); – –x)
  25. #define REP(x, n) for(int x = 0; x < (n); ++x)
  26. #define VAR(v, n) __typeof(n) v = (n)
  27. #define ALL(c) (c).begin(), (c).end()
  28. #define SIZE(x) ((int)(x).size())
  29. #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
  30. #define PB push_back
  31. #define ST first
  32. #define ND second
  33.  
  34. static int AsciiSpecialCharacters[32] = {33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47 ,58, 59, 60, 61, 62, 63, 64, 91, 92, 93, 94, 95, 96, 123, 124, 125, 126};
  35.  
  36. void print_msg(string & function, string & msg);
  37. bool is_login_and_password_are_correct(string &, string &);
  38. bool is_lenght_correct(string &, string &);
  39. bool is_login_contain_illegal_char(string &);
  40. bool is_password_meet_criteria(string &);
  41. void print_result(vector<string> & arr);
  42. void print_accounts(map<string, string> & account);
  43. bool is_account_exist(map<string,string> & account, string &);
  44. string change_to_lower_case(string &);
  45. void create_account(map<string, string> & account, string & login, string & password);
  46. bool is_password_correct(map<string,string> & accounts, string & login, string & password);
  47.  
  48.  
  49.  
  50.  
  51. int main(){
  52.  
  53. int loginAttemps;
  54. int registerAttemps;
  55. map<string, string> account;
  56. string login;
  57. string password;
  58. vector<string> result;
  59.  
  60. cout << "register ";
  61. cin >> registerAttemps;
  62. REP(i, registerAttemps){
  63. cin >> login >> password;
  64. if(is_login_and_password_are_correct(login, password)){
  65. if(!is_account_exist(account, login)){
  66. create_account(account, login, password);
  67. result.PB("Zarejestrowano");
  68. }else
  69. {
  70. result.PB("Login zajety");
  71. }
  72. }else{
  73.  
  74. result.PB("Blad");
  75. }
  76. }
  77.  
  78. cout << "login ";
  79. cin >> loginAttemps;
  80. REP(i, loginAttemps){
  81. cin >> login >> password;
  82. if(is_account_exist(account, login)){
  83. if(is_password_correct(account, login, password)){
  84. result.PB("Zalogowano");
  85. }else
  86. {
  87. result.PB("Zle haslo");
  88. }
  89. }else
  90. {
  91. result.PB("Konto nie istnieje");
  92. }
  93.  
  94. }
  95.  
  96.  
  97. cout << "register ";
  98. cin >> registerAttemps;
  99. REP(i, registerAttemps){
  100. cin >> login >> password;
  101. if(is_login_and_password_are_correct(login, password)){
  102. if(!is_account_exist(account, login)){
  103. create_account(account, login, password);
  104. result.PB("Zarejestrowano");
  105. }else
  106. {
  107. result.PB("Login zajety");
  108. }
  109. }else{
  110.  
  111. result.PB("Blad");
  112. }
  113. }
  114.  
  115.  
  116.  
  117. cout << "login ";
  118. cin >> loginAttemps;
  119. REP(i, loginAttemps){
  120. cin >> login >> password;
  121. if(is_account_exist(account, login)){
  122. if(is_password_correct(account, login, password)){
  123. result.PB("Zalogowano");
  124. }else
  125. {
  126. result.PB("Zle haslo");
  127. }
  128. }else
  129. {
  130. result.PB("Konto nie istnieje");
  131. }
  132.  
  133. }
  134.  
  135.  
  136.  
  137. print_result(result);
  138.  
  139.  
  140.  
  141. return 0;
  142. }
  143. void print_msg(string & function, string & msg){
  144. cout << function << " " << msg << "\n";
  145. }
  146.  
  147. void print_result(vector<string> & arr){
  148. FOR(i, 0, SIZE(arr)-1){
  149. cout << arr[i] << "\n";
  150. }
  151. }
  152. void print_accounts(map<string, string> & account){
  153. cout << "\n";
  154. FOREACH(it, account){
  155. cout << it->first << " " << it->second << "\n";
  156. }
  157. }
  158. bool is_lenght_correct(string & login, string & password){
  159.  
  160. if(login.length()<3 || login.length()>12){
  161. return false;
  162. }
  163. if(password.length()<5 || password.length()>15){
  164. return false;
  165. }
  166.  
  167.  
  168. return true;
  169. }
  170. bool is_login_contain_illegal_char(string & login){
  171.  
  172. int character;
  173. FOR(i, 0, login.length()-1){
  174. character = (int)login[i];
  175.  
  176. FOR(j, 0, 32){
  177. if(character == AsciiSpecialCharacters[j]){
  178. // cout << "Special character detected " << a << endl;
  179. return false;
  180. }
  181. }
  182. }
  183. return true;
  184. }
  185. /* these criteria is:
  186. - number
  187. - special character
  188. - character in upper case
  189. - character in lower case
  190. */
  191. bool is_password_meet_criteria(string & password){
  192. bool numberOccurence = false;
  193. bool specialCharacterOccurence = false;
  194. bool upperCaseCharacterOccurence = false;
  195. bool lowerCaseCharacterOccurence = false;
  196.  
  197. FOR(i, 0, password.length()){
  198. if(numberOccurence == false) FOR(n, 48, 57){
  199. if((char)(n) == password[i]){
  200. numberOccurence = true;
  201. }
  202. }
  203. if(specialCharacterOccurence == false) FOR(j, 0, 32){
  204. if(AsciiSpecialCharacters[j] == password[i]){
  205. specialCharacterOccurence = true;
  206. }
  207. }
  208. if(upperCaseCharacterOccurence == false) FOR(c, 65, 90){
  209. if((char)(c) == password[i]){
  210. upperCaseCharacterOccurence = true;
  211. }
  212. }
  213. if(lowerCaseCharacterOccurence == false) FOR(c, 97, 122){
  214. if((char)(c) == password[i]){
  215. lowerCaseCharacterOccurence = true;
  216. }
  217. }
  218. }
  219. if(!(numberOccurence && specialCharacterOccurence && upperCaseCharacterOccurence && lowerCaseCharacterOccurence)){
  220. return false;
  221. }
  222. return true;
  223. }
  224. bool is_login_and_password_are_correct(string & login, string & password){
  225. if(!is_lenght_correct(login, password)){
  226. return false;
  227. }
  228. if(!is_login_contain_illegal_char(login)){
  229. return false;
  230. }
  231. if(!is_password_meet_criteria(password)){
  232. return false;
  233. }
  234. return true;
  235. }
  236. string change_to_lower_case(string & text){
  237.  
  238. string lower_text = text;
  239.  
  240. FOR(i, 0, text.length()){
  241. lower_text[i] = tolower(lower_text[i]);
  242. }
  243. return lower_text;
  244. }
  245. void create_account(map<string, string> & account, string & login, string & password){
  246.  
  247. string lowerCaseLogin = change_to_lower_case(login); // changing login to lower case because it doenst make an odds
  248. account[lowerCaseLogin] = password;
  249.  
  250. }
  251. bool is_account_exist(map<string,string> & accounts, string & login){
  252. string lowerCaseLogin = change_to_lower_case(login);
  253. FOREACH(it, accounts){
  254. if((it->first.compare(lowerCaseLogin))==0){
  255.  
  256. return true;
  257. }
  258. }
  259. return false;
  260. }
  261.  
  262. bool is_password_correct(map<string,string> & accounts, string & login, string & password){
  263. string lowerCaseLogin = change_to_lower_case(login);
  264. FOREACH(it, accounts){
  265. if((it->first.compare(lowerCaseLogin))==0){
  266. if((it->second.compare(password))==0){
  267. return true;
  268. }
  269. }
  270. }
  271. return false;
  272. }
Success #stdin #stdout 0s 4356KB
stdin
register 3
bajtek13 Haslo123@
BITEK 123456789
bajtek13 bajteK55%
login 5
bajtek13 bajteK55%
bajtek13 Haslo123@
BITEK 123456789
bajtocjusz haselko49
bitariusz 123haSlo!@#
register 1
BITEK Dobrehaslo1!
login 1
BITEK Dobrehaslo1!
stdout
register login register login