1220: Word relay

 (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~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值