fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. string longestSubstring(string s) {
  7. int n = s.length();
  8. int i = 0, j = 0;
  9. string maxStr;
  10. vector<bool> exist(256);
  11. while (j < n) {
  12. if (exist[s[j]]) {
  13. if (maxStr.length() < (j-i)) {
  14. maxStr = s.substr(i, j-i);
  15. }
  16. while (s[i] != s[j]) {
  17. exist[s[i]] = false;
  18. i++;
  19. }
  20. i++;
  21. j++;
  22. } else {
  23. exist[s[j]] = true;
  24. j++;
  25. }
  26. }
  27. if (maxStr.length() < (n-i)) {
  28. maxStr = s.substr(i, n-i);
  29. }
  30. return maxStr;
  31. }
  32.  
  33. int main() {
  34. cout << longestSubstring("abcaadafghae") << endl;
  35. return 0;
  36. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
dafgh