- 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;
#include<iostream>
#include<string>
#include<set>
#include<cstring>
using namespace std;
set<string> s;
const int maxn = 1E4 + 10 ;
int dp[maxn];
int main(){
string str;
cin>>str;
memset(dp,0,sizeof(dp));
str = str.substr(5);
int n = str.length();
string tmp;
dp[n] = 1;
for ( int i = n - 1 ; I >= 0 ; -- i ) {
for(int l = 2 ; l <= 3 ; ++l){
tmp = str.substr(i, l);
if((Str.Find(tmp,I+L) !=I+L||DP[I+ 5] ) &&DP[I+L] ){
s.insert(tmp);
dp[i] = 1;
}
}
}
cout<<s.size()<<"\n";
for(set<string>::iterator its = s.begin(); its != s.end();++its){
cout<<*its<<"\n";
}
return 0;
}
From for notes (Wiz)
[2016-05-04] [Codeforces] [666a-reberland Linguistics]