[2016-05-04] [Codeforces] [666a-reberland Linguistics]

Source: Internet
Author: User

    • Time: 2016-05-04-16:51:34
    • Title Number: [2016-05-04][codeforces][666a-reberland linguistics]
    • Title: A word consists of a root with a length of not less than 5 and a number of suffixes of 2 or 3, and two adjacent suffixes can not be the same, given a word, ask the total number of suffixes
    • Analysis:
      • A DP-like thought, dp[i] Indicates whether the first position to the end of the string can be divided into a legal suffix,
      • So dp[i] = Dp[i +t]∧ (dp[i + 5]∨ substring (i,t)! = substring (i + t, T)); t = = 2 or 3
      • That is, from the backward sweep, each additional character, it is determined by the length of the 2 and 3 is the suffix and the remaining part of the suffix is legitimate
      • Is the new suffix legal,
        • With this suffix, the remainder can still be divided into 2 or 3 suffixes.
        • This suffix is not the same as the next suffix (as long as there is a different division method can be)
          • This suffix is the same as the next suffix length: direct comparison
          • The length is not the same, obviously set up, but to be in the forward number of 5 remaining can be legally divided, that is, dp[i+ 5] for the real situation.
    • Problems encountered:
      • When judging whether the current suffix and the next suffix are the same, if the substring is used to compare, I + 2 > length will be wrong at the beginning.
      • The workaround is not to generate substrings, but to find the location of the next and its same string through the Find function
      • Initialize dp[length] = 0;
  
 
  1. #include<iostream>
  2. #include<string>
  3. #include<set>
  4. #include<cstring>
  5. using namespace std;
  6. set<string> s;
  7. const int maxn = 1E4 + 10 ;
  8. int dp[maxn];
  9. int main(){
  10. string str;
  11. cin>>str;
  12. memset(dp,0,sizeof(dp));
  13. str = str.substr(5);
  14. int n = str.length();
  15. string tmp;
  16. dp[n] = 1;
  17. for ( int i = n - 1 ; I >= 0 ; -- i ) {
  18. for(int l = 2 ; l <= 3 ; ++l){
  19. tmp = str.substr(i, l);
  20. if((Str.Find(tmp,I+L) !=I+L||DP[I+ 5] ) &&DP[I+L] ){
  21. s.insert(tmp);
  22. dp[i] = 1;
  23. }
  24. }
  25. }
  26. cout<<s.size()<<"\n";
  27. for(set<string>::iterator its = s.begin(); its != s.end();++its){
  28. cout<<*its<<"\n";
  29. }
  30. return 0;
  31. }


From for notes (Wiz)

[2016-05-04] [Codeforces] [666a-reberland Linguistics]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.