POJ3080-Blue Jeans
转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1309012790
大致题意:
就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10
规定:
1、 最长公共串长度小于3不输出
2、 若出现等长的最长的子串,则输出字典序最小的串
解题思路:
纠结了几个月放着没做的题目。。
一直以为要用KMP或者后缀数组来做。。。
然后我就拼命学后缀。。。
今天偶然发现直接 暴力 能够达到0ms的效果= =
所以。。。暴力吧。。。不愧为初级的题。。。
暴力思想很简单:
开二维DNA[][]保存所有DNA序列
1、 以DNA[0]为母版,顺次截取60个长度length=1的子串dna[],检查其他DNA[i]是否都有子串dna,若是则把dna[]复制到obj[],否则枚举下一个长度length的子串;若obj与新的dna等长,则比较两者字典序,当dna字典序小时,复制到obj
2、 第1步循环60次后length+1。
顺次截取59个长度length=2的子串dna[],重复1的操作更新obj。。
3、 第2步循环59次后length+1。
顺次截取58个长度length=3的子串dna[],继续。。
...........
60、第59步循环2次后length+1。
顺次截取1个长度length=60的子串dna[],继续重复操作更新obj。。
61、输出obj
Source修正:
http://acm2006.cct.lsu.edu/problems/
1 //Memory Time
2 //248K 16MS
3
4 #include<iostream>
5 #include<string.h>
6 using namespace std;
7
8 const int len=60;
9
10 int main(int i,int j)
11 {
12 int test;
13 cin>>test;
14 for(int t=1;t<=test;t++)
15 {
16 int n; //DNA个数
17 cin>>n;
18 char** DNA=new char*[n];
19 for(int p=0;p<n;p++)
20 {
21 DNA[p]=new char[len+1];
22 cin>>DNA[p];
23 }
24
25 char obj[len+1]; //所有DNA的公共串
26 int StrLen=0; //最长公共串长度
27 int length=1; //当前枚举的公共串长度
28
29 for(i=0;;i++) //枚举公共串dna[]
30 {
31 /*截取DNA[0][]中以pi为起点,长度为length的子串dna[]*/
32 char dna[len+1];
33 int pi=i;
34 if(pi+length>len)
35 {
36 length++;
37 i=-1;
38 if(length>len)
39 break;
40 continue;
41 }
42 for(j=0;j<length;j++)
43 dna[j]=DNA[0][pi++];
44 dna[j]='\0';
45
46 /*检查其他DNA[][]是否都存在字符串dna[]*/
47 bool flag=true;
48 for(int k=1;k<n;k++)
49 if(!strstr(DNA[k],dna)) //存在一个DNA不含有dna[]
50 {
51 flag=false;
52 break;
53 }
54
55 /*确认最大公共串*/
56 if(flag)
57 {
58 if(StrLen<length)
59 {
60 StrLen=length;
61 strcpy(obj,dna);
62 }
63 else if(StrLen==length)
64 {
65 if(strcmp(obj,dna)>0) //存在相同长度的公共串时,取最小字典序的串
66 strcpy(obj,dna);
67 }
68 }
69 }
70
71 if(StrLen<3)
72 cout<<"no significant commonalities"<<endl;
73 else
74 cout<<obj<<endl;
75
76 delete DNA;
77 }
78 return 0;
79 }
11
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
2
GATGATGATGATGATGATGATGATGATGATGATGATGATGATGATGATGATGATGATGAT
GATTGATTGATTGATTGATTGATTGATTGATTGATTGATTGATTGATTGATTGATTGATT
10
GATGATGATGATGATGATGATGATGATGATGATGATGATGATGATGATGATGATGATGAT
GATTGATTGATTGATTGATTGATTGATTGATTGATTGATTGATTGATTGATTGATTGATT
GATTTGATTTGATTTGATTTGATTTGATTTGATTTGATTTGATTTGATTTGATTTGATTT
GATTTTGATTTTGATTTTGATTTTGATTTTGATTTTGATTTTGATTTTGATTTTGATTTT
AGATAGATAGATAGATAGATAGATAGATAGATAGATAGATAGATAGATAGATAGATAGAT
AAGATAAGATAAGATAAGATAAGATAAGATAAGATAAGATAAGATAAGATAAGATAAGAT
AAAGATAAAGATAAAGATAAAGATAAAGATAAAGATAAAGATAAAGATAAAGATAAAGAT
CGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGAT
CCGATCCGATCCGATCCGATCCGATCCGATCCGATCCGATCCGATCCGATCCGATCCGAT
CCCGATCCCGATCCCGATCCCGATCCCGATCCCGATCCCGATCCCGATCCCGATCCCGAT
10
GATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATC
GCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTA
TAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGC
CGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGAT
GTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGTGT
GAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGA
GCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGC
ATATATATATATATATATATATATATATATATATATATATATATATATATATATATATAT
ACACACACACACACACACACACACACACACACACACACACACACACACACACACACACAC
TCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTCTC
10
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGGGG
4
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
10
AAAAAATAAAAAATAAAAAATAAAAAATAAAAAATAAAAAATAAAAAATAAAAAATAAAA
AAAAACAAAAACAAAAACAAAAACAAAAACAAAAACAAAAACAAAAACAAAAACAAAAAC
AAAAAGAAAAAGAAAAAGAAAAAGAAAAAGAAAAAGAAAAAGAAAAAGAAAAAGAAAAAG
AAAAATAAAAATAAAAATAAAAATAAAAATAAAAATAAAAATAAAAATAAAAATAAAAAT
AAAACAAAACAAAACAAAACAAAACAAAACAAAACAAAACAAAACAAAACAAAACAAAAC
AAAAGAAAAGAAAAGAAAAGAAAAGAAAAGAAAAGAAAAGAAAAGAAAAGAAAAGAAAAG
AAAATAAAATAAAATAAAATAAAATAAAATAAAATAAAATAAAATAAAATAAAATAAAAT
AAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAAC
AAATAAATAAATAAATAAATAAATAAATAAATAAATAAATAAATAAATAAATAAATAAAT
AAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAG
2
GATGATGCATCATGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGACTACTAA
GATGATCATCATACTACTCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
Sample Output
no significant commonalities
AGATAC
CATCATCAT
TGAT
GAT
no significant commonalities
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
no significant commonalities
AAA
ACTACT