(http://ybt.ssoier.cn:8088/problem_show.php?pid=1220)
Let's fir!
We found that the overlapping parts were covered as a whole in this question. I use lilen [i] [j] to represent the same part in i and j.
st find the one with the same starting letter and the last input letter, record it, and enter the recursive function.
The specific function code is:
int n;
int uscnt[30];//This is usage count.
int maxlen;//This is length of the longest dragon.
int len;//This is the current length of the dragon.
int lilen[30][30];//This is i's and j's overlapping part.
string val[30],start;//This is input.
//This function is to find the overlapping parts of s and s1.
int overla(string s,string s1){
for(int i=s.size()-1;i>=s.size()-min(s.size(),s1.size());i--){
int k=i,m=0;
for(;k<s.size();k++,m++){
if(s[k]!=s1[m]){
goto aa;
}
}
return m;
aa:;
}
return 0;
}
//This function initializes lilen.
void liass(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
lilen[i][j]=overla(val[i],val[j]);
}
}
}
//This function is used to find the longest dragon.
void findrag(int idx){
if(len>maxlen){
maxlen=len;
}
for(int i=0;i<n;i++){
if(uscnt[i]<2&&lilen[idx][i]>0){
uscnt[i]++;
len+=(val[i].size()-lilen[idx][i]);
findrag(i);
uscnt[i]--;
len-=(val[i].size()-lilen[idx][i]);
}
}
}
For everyone, backtracking should be a skill. However, the first two functions were mentioned earlier and were used to initialize lilen [] []. After reading the annotation, you should understand.
It's not simple in int main() either. After inputting, one by one, you need to check if the beginning and last of the words are the same, and if they are the same, you also need to "backtrack" them and output them.
The specific function code is:
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>val[i];
}
cin>>start;
liass();
for(int i=0;i<n;i++){
if(val[i][0]==start[0]){
len=val[i].size();
uscnt[i]++;
findrag(i);
uscnt[i]--;
}
}
cout<<maxlen;
return 0;
}
Some variables and arrays are not mentioned in the text, but are mentioned in the code comments.
All codes are:
#include<bits/stdc++.h>
using namespace std;
int n;
int uscnt[30];//This is usage count.
int maxlen;//This is length of the longest dragon.
int len;//This is the current length of the dragon.
int lilen[30][30];//This is i's and j's overlapping part.
string val[30],start;//This is input.
//This function is to find the overlapping parts of s and s1.
int overla(string s,string s1){
for(int i=s.size()-1;i>=s.size()-min(s.size(),s1.size());i--){
int k=i,m=0;
for(;k<s.size();k++,m++){
if(s[k]!=s1[m]){
goto aa;
}
}
return m;
aa:;
}
return 0;
}
//This function initializes lilen.
void liass(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
lilen[i][j]=overla(val[i],val[j]);
}
}
}
//This function is used to find the longest dragon.
void findrag(int idx){
if(len>maxlen){
maxlen=len;
}
for(int i=0;i<n;i++){
if(uscnt[i]<2&&lilen[idx][i]>0){
uscnt[i]++;
len+=(val[i].size()-lilen[idx][i]);
findrag(i);
uscnt[i]--;
len-=(val[i].size()-lilen[idx][i]);
}
}
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>val[i];
}
cin>>start;
liass();
for(int i=0;i<n;i++){
if(val[i][0]==start[0]){
len=val[i].size();
uscnt[i]++;
findrag(i);
uscnt[i]--;//Len does not need to be 'backtracked' because it will be modified when executed again later.
}
}
cout<<maxlen;
return 0;
}
This is my code, as I am new here and may not write well. Please give me more guidance. Thank you~