30: Character Token
-
Total time limit:
-
1000ms
-
Memory Limit:
-
65536kB
-
Describe
-
There are two rings composed of characters. Please write a program that calculates the length of the longest contiguous public string on the token of the two characters. For example, the strings "ABCEFAGADEGKABUVKLM" are joined together to form a ring; the string "Madjkluvkl" is joined together to form a second ring; "Uvklma" is a contiguous public string of these two rings.
-
Input
-
A row that contains two strings that correspond to one character token. The two strings are separated by a single space. The string length does not exceed 255, and it does not contain whitespace such as spaces.
-
Output
-
Outputs an integer that represents the length of the longest public string on the token of the two words.
-
Sample input
-
ABCEFAGADEGKABUVKLM MADJKLUVKL
-
Sample output
-
6
Ideas:
Dynamic return;
Come on, on the code:
#include <cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;intlen_1,len_2,ans,cur_ans=0, dp[520][520];BOOLci[520][520];stringword_1,word_2;intMain () {CIN>>word_1>>word_2; Len_1=word_1.length (), len_2=word_2.length (); Ans=min (len_1,len_2); //for (int i=0;i<len_1;i++) word_1[i+len_1]=word_1[i]; //for (int i=0;i<len_2;i++) word_2[i+len_2]=word_2[i];word_1+=word_1,word_2+=word_2; Len_2*=2, len_1*=2; for(intI=0; i<len_1;i++) { for(intj=0; j<=len_2;j++) { if(Word_1[i]==word_2[j]) ci[i+1][j+1]=true, dp[i+1][j+1]++; } } for(intI=1; i<=len_1;i++) { for(intj=1; j<=len_2;j++) { if(Ci[i][j]) {if(ci[i-1][j-1]) Dp[i][j]=max (dp[i][j],dp[i-1][j-1]+1); Cur_ans=Max (cur_ans,dp[i][j]); }}} ans=min (Ans,cur_ans); printf ("%d\n", ans); return 0;}
AC Diary--Character token Openjudge 1.7 30