北京林业大学“计蒜客”杯程序设计竞赛 网络赛 D-A letter from Chensg【最长公共子序列LCS】

Can it really be 2 years since I first saw you? I still remember, vividly, 2 years ago, from the moment I saw you smile, as you were walking out of the classroom and turned your head back, with the soft sunset glow shining on your rosy cheek, I knew, I knew that I was already drunk on you. Then, after several months' observation and prying, your grace and your wisdom, your attitude to life and your aspiration for future were all strongly impressed on my memory. You were the glamorous and sunny girl whom I always dream of to share the rest of my life with. Alas, actually you were far beyond my wildest dreams and I had no idea about how to bridge that gulf between you and me. So I schemed nothing but to wait, to wait for an appropriate opportunity. Till now, I realize I am such an idiot that one should create the opportunity and seize it instead of just waiting?

Nowadays, you are planning to fly far far away, to pursue your future and fulfill your dreams. Perhaps we will not speak with each other very often if without fate and luck. Sometimes, I wandered around corridor hoping to meet you there by chance. But contradictorily, your appearance must quicken my heartbeat and my clumsy tongue might be not able to belch out a word. I cannot remember how many times I have passed your dormitory , and each time aspired to see you appear in the corridor. I cannot remember how many times this idea comes to my mind: call her out to have dinner or at least a conversation. But each time, thinking of your excellence and my commonness, the predominance of timidity over courage drove me leave silently?

Graduation, means the end of life in university, the end of these glorious, romantic years. Your lovely smile which is my original incentive to work hard and this unrequited love will be both sealed as a memory in the deep of my heart and my mind. Graduation, also means a start of new life, a footprint on the way to bright prospect. I truly hope you will be happy everyday abroad and everything goes well. Meanwhile, I will try to get out from puerility and become more sophisticated. To pursue my own love and happiness here in reality will be my ideal I never desert?

Good luck, my princess!?

If someday, somewhere, we have a chance to gather, even as gray-haired man and woman, at that time, I hope we can be good friends to share this memory proudly to relight the youthful and joyful emotions. If this chance never comes, I wish I were the stars in the sky and twinkling in your window, to bless you far away, as friends, to accompany you every night, sharing the sweet dreams or going through the nightmares together?

Here comes the problem: Assume the star is represented by a string sequence only including four different characters 'S', 'T', 'A' and 'R'. Of course,each character will appear many times.Your task is to write a program that will find commonalities amongst string sequces. Given a set of star sequences,Please determine the longest common subsequence that occurs in all of the sequences.

Input:

Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:

A single positive integer mm indicating the number of string sequences in this dataset. m lines each containing a single sequence consisting of 6060 characters, 2 \leq m \leq 102m10.

Output:

For each dataset in the input, output the longest subsequence common to all of the given sequences. If the longest common subsequence is less than three characters in length, display the string "No significant commonalities" instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.

样例1

输入:

3
2
SATARRASATARRASATARRASATARRASATARRASATARRASATARRASATARRASATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
SATARRASATARRASATARRASATARRASATARRASATARRASATARRASATARRASATA
SATARTASATARTASATARTASATARTAAASSAAASSSAAAASSSSAAAAASSSSSAAAA
SATARRASATARRASATARRASATARRAAASSAAASSSAAAASSSSAAAAASSSSSAAAA
3
RATRATRATRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
ARATRATRATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AARATRATRATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

输出:

No significant commonalities
ASATAR
RATRATRAT

解题思路:

最长公共子序列。

#include <iostream>  
#include <string>
using namespace std; 

string getLCSLength(string &s, string &t);

int main()
{
	int t; scanf("%d", &t);
	while(t--)
	{
		string s, b;
		int n;  scanf("%d", &n); 
		cin >>s;
		for(int i=1;  i<n; i++)
		{
			cin >>b;
			s=getLCSLength(s, b);
		}
		if(s.length()< 3)
			printf("No significant commonalities\n");
		else
		cout << s<< endl;
	}
	return 0;
}

string getLCSLength(string &s, string &t)
{
	int p = s.length();
	int q = t.length();

	string **num = new string *[p];  
    for(int i=0;i<p;i++)    
    {    
        num[i] = new string[q];  
    }   

	char char1 = '\0';
	char char2 = '\0';

	int len = 0;
	string lcs = "" ;

	for(int i=0; i<p; i++)
	{
		for (int j=0; j<t.length(); j++)
		{
			char1 = s.at(i);
			char2 = t.at(j);
			if(char1!=char2)
			{
				num[i][j] = "" ;
			}
			else
			{
				if (i==0||j==0)
				{
					num[i][j]=char1;
				}
				else
				{
					num[i][j]=num[i-1][j-1]+char1;
				}
				if(num[i][j].length()>len)
				{
					len = num[i][j].length() ;
					lcs = num[i][j];
				}
				else if(num[i][j].length()==len)
				{
					lcs =num[i][j]; 
					//lcs = lcs + "," + num[i][j];
				}
			}
		}
	}

	for(int i=0;i<p;i++)    
    {    
        delete[] num[i]; 
    } 
	delete[] num;

	return lcs;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值