小言_互联网的博客

E.密码(算法选修)

234人阅读  评论(0)

松塔先生决定在他的电脑上下载原神来玩,但是他的队友灵妹妹觉着玩游戏实在是太浪费时间了,于是灵妹妹给松塔先生的原神下载界面上了锁,并丢给松塔先生一串字符。

现在有三种子串:
1.子串是该字符串的前缀
2.子串是该字符串的后缀
3.子串既不是该字符串的前缀也不是该字符串的后缀
密码就是满足这三种条件的最长子串。

即密码既是字符串的前缀,又是字符串的后缀,且在字符串中以非前后缀的形式出现过。
但是松塔先生太懒了,宁可睡觉也不解密码,请你帮助他。
特别的,由于灵妹妹特别的狡猾,密码可能根本就不在这串字符串里,如果是这样,输出"Play what play,Roll to study"
 

Input
多行由小写英文字母组成的字符串,长度n(1<=n<=1e6)
Output

多行字符串,原神下载页面的密码或者"Play what play,Roll to study"

末尾有换行

Sample Input

abcdefabcghaabc

abc

Sample Output

abc

Play what play,Roll to study


  
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. ios:: sync_with_stdio( false), cin. tie( nullptr), cout. tie( nullptr);
  6. string a,b,c,d;
  7. while(cin>>a)
  8. {
  9. b=a;
  10. int k= 0,num= 0,kb= -1;
  11. reverse(b. begin(),b. end());
  12. string A,B;
  13. A. empty();
  14. B. empty();
  15. for( int i= 0;i<( int)a. length() -1;i++)
  16. {
  17. A+=a[i];
  18. B+=b[i];
  19. for( int j= 0;j<( int)A. length();j++)
  20. {
  21. if(a[j]==b[A. length()-j -1])
  22. k= 0;
  23. else
  24. {
  25. k= 1;
  26. break;
  27. }
  28. }
  29. if(k== 0)
  30. {
  31. c=A;
  32. int pos=a. find(c, 1);
  33. if((pos+c. length() -1)<(a. length() -1))
  34. {
  35. int len=c. length();
  36. if(len>kb)
  37. {
  38. kb=len;
  39. d=c;
  40. num= 1;
  41. }
  42. }
  43. else
  44. continue;
  45. }
  46. }
  47. if(num== 0)
  48. cout<< "Play what play,Roll to study"<<endl;
  49. else
  50. cout<<d<<endl;
  51. }
  52. return 0;
  53. }

 


转载:https://blog.csdn.net/q619718/article/details/127947709
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场