ReverseSubstring | |
You are given a String input. You are to find the longest substring of input such that the reversal of the substring is also a substring of input. In case of a tie, return the string that occurs earliest in input. Definition 給你一個字符串,你再生成一個顛倒的字符串,從原串中找出任意子串能同時存在顛倒的字符串中, 求出最長子串 Class: ReverseSubstring Method: findReversed Parameters: String Returns: String Method signature: String findReversed(String input) (be sure your method is public) 類ReverseSubstring方法 public String findReversed(String input) Notes - The substring and its reversal may overlap partially or completely. - The entire original string is itself a valid substring (see example 4). Constraints - input will contain between 1 and 50 characters, inclusive. - Each character of input will be an uppercase letter ('A'-'Z'). Examples 0) "XBCDEFYWFEDCBZ" Returns: "BCDEF" We see that the reverse of BCDEF is FEDCB, which appears later in the string. 顛倒的字符串為"ZBCDEFWYFEDCBX",原串中BCDEF也是顛倒的字符串的子串,并且為最長的 1) "XYZ" Returns: "X" The best we can do is find a one character substring, so we implement the tie-breaker rule of taking the earliest one first. 2) "ABCABA" Returns: "ABA" The string ABA is a palindrome (it's its own reversal), so it meets the criteria. 3) "FDASJKUREKJFDFASIREYUFDHSAJYIREWQ" Returns: "FDF" 4) "ABCDCBA" Returns: "ABCDCBA" Here, the entire string is its own reversal. This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. |
評論
下個星期的資格賽說明是:
The Qualification Round will be open from Tuesday, September 5, at 12:00 PM (noon) EDT (GMT/UTC -4) through Wednesday, September 6, at 12:00 PM (noon) EDT (GMT/UTC -4). During this 24-hour period, each competitor must complete one randomly generated problem set. All competitors will receive a score for their performance on that one problem set.
如果你已經注冊參賽,在確認郵件的 PRACTICING FOR THE EVENT 一段中有關于賽前練手的信息,我收集的題目也可以用來練手。如果你還沒有注冊就抓緊注冊吧,不注冊是不能啟用competition arena來練手的。 回復 更多評論
The Qualification Round will be open from Tuesday, September 5, at 12:00 PM (noon) EDT (GMT/UTC -4) through Wednesday, September 6, at 12:00 PM (noon) EDT (GMT/UTC -4). During this 24-hour period, each competitor must complete one randomly generated problem set. All competitors will receive a score for their performance on that one problem set.
如果你已經注冊參賽,在確認郵件的 PRACTICING FOR THE EVENT 一段中有關于賽前練手的信息,我收集的題目也可以用來練手。如果你還沒有注冊就抓緊注冊吧,不注冊是不能啟用competition arena來練手的。 回復 更多評論
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class ReverseSubstring {
private static String original="";
public static void main(String[] args) throws IOException {
String s=input();
String s1=reversed(s);
System.out.println(findReversed(Reversedsub(s),Reversedsub( s1))+"結果");
}
static String findReversed(Hashtable orig,Hashtable reversed){
Enumeration enum=orig.elements();
String result=original.substring(0,1);
while(enum.hasMoreElements()){
String s=(String)enum.nextElement();
if( s.equals(reversed.get(s))){
if(s.length()>=result.length()){
result=s;
}
}
}
return result;
}
static Hashtable Reversedsub(String s){
String originals=s;
Hashtable hash=new Hashtable();
for(int m=0;m<originals.length();m++){
for(int i=0;i<originals.length()-m;i++){
String sub= originals.substring(i,i+m+1);
hash.put(sub,sub);
}
}
return hash;
}
static String reversed(String s){
int len=0;
char[] original=s.toCharArray();
len= original.length;
char[] rever=new char[len];
int m=len-1;
for( int i=0;i<=len-1;i++){
rever[i]=original[m];
m--;
}
return new String(rever);
}
static String input() throws IOException{
int first=0;
boolean stop=true;
while(stop){
if(first!=0){
original="輸入出錯請重新輸入";
}
else{
original="請輸入數據";
}
System.out.println(original);
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
original=input.readLine();
if(original.length()==0||original.length()>50){
original="輸入出錯請重新輸入";
}
else{
stop=false;
}
first+=1;
}
return original;
}
} 回復 更多評論
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class ReverseSubstring {
private static String original="";
public static void main(String[] args) throws IOException {
String s=input();
String s1=reversed(s);
System.out.println(findReversed(Reversedsub(s),Reversedsub( s1))+"結果");
}
static String findReversed(Hashtable orig,Hashtable reversed){
Enumeration enum=orig.elements();
String result=original.substring(0,1);
while(enum.hasMoreElements()){
String s=(String)enum.nextElement();
if( s.equals(reversed.get(s))){
if(s.length()>=result.length()){
result=s;
}
}
}
return result;
}
static Hashtable Reversedsub(String s){
String originals=s;
Hashtable hash=new Hashtable();
for(int m=0;m<originals.length();m++){
for(int i=0;i<originals.length()-m;i++){
String sub= originals.substring(i,i+m+1);
hash.put(sub,sub);
}
}
return hash;
}
static String reversed(String s){
int len=0;
char[] original=s.toCharArray();
len= original.length;
char[] rever=new char[len];
int m=len-1;
for( int i=0;i<=len-1;i++){
rever[i]=original[m];
m--;
}
return new String(rever);
}
static String input() throws IOException{
int first=0;
boolean stop=true;
while(stop){
if(first!=0){
original="輸入出錯請重新輸入";
}
else{
original="請輸入數據";
}
System.out.println(original);
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
original=input.readLine();
if(original.length()==0||original.length()>50){
original="輸入出錯請重新輸入";
}
else{
stop=false;
}
first+=1;
}
return original;
}
} 回復 更多評論
@小貓魚
bool Strstr(const char cStr1[], const char cStr2[], int Start, int Count)
{
char *SubString = (char*)malloc(Count + 1);
memcpy(SubString, cStr2+Start, Count);
SubString[Count] = '\0';
char *find = strstr(cStr1, SubString);
free(SubString);
return find == NULL?false:true;
}
int GetSubString(char cStr[], int iSeatCount)
{
int left=0;
int right=iSeatCount-1;
int startindex = 0;
int last = 0;
int templast = 0;
char ch='\0';
char *ReverseSubstring = (char*)malloc(iSeatCount + 1);
memcpy(ReverseSubstring, cStr, iSeatCount + 1);
while(left <= right)
{
ch = ReverseSubstring[left];
ReverseSubstring[left] = ReverseSubstring[right];
ReverseSubstring[right] = ch;
left++;
right = iSeatCount-1-left;
}
ReverseSubstring[iSeatCount] = '\0';
for (int i=0; i<iSeatCount; i++)
{
templast = 1;
while (Strstr(ReverseSubstring, cStr, i, templast) && templast <= iSeatCount - i)
{
templast++;
}
if ((templast - 1) > last)
{
startindex = i;
last = templast - 1;
}
}
printf("The StartPostion is %d and the last number is %d.\n", startindex, last);
printf("The Max Common String is ");
for (i=0; i<last; i++)
{
printf("%c", *(cStr+startindex+i));
}
printf("\n");
return last;
} 回復 更多評論
bool Strstr(const char cStr1[], const char cStr2[], int Start, int Count)
{
char *SubString = (char*)malloc(Count + 1);
memcpy(SubString, cStr2+Start, Count);
SubString[Count] = '\0';
char *find = strstr(cStr1, SubString);
free(SubString);
return find == NULL?false:true;
}
int GetSubString(char cStr[], int iSeatCount)
{
int left=0;
int right=iSeatCount-1;
int startindex = 0;
int last = 0;
int templast = 0;
char ch='\0';
char *ReverseSubstring = (char*)malloc(iSeatCount + 1);
memcpy(ReverseSubstring, cStr, iSeatCount + 1);
while(left <= right)
{
ch = ReverseSubstring[left];
ReverseSubstring[left] = ReverseSubstring[right];
ReverseSubstring[right] = ch;
left++;
right = iSeatCount-1-left;
}
ReverseSubstring[iSeatCount] = '\0';
for (int i=0; i<iSeatCount; i++)
{
templast = 1;
while (Strstr(ReverseSubstring, cStr, i, templast) && templast <= iSeatCount - i)
{
templast++;
}
if ((templast - 1) > last)
{
startindex = i;
last = templast - 1;
}
}
printf("The StartPostion is %d and the last number is %d.\n", startindex, last);
printf("The Max Common String is ");
for (i=0; i<last; i++)
{
printf("%c", *(cStr+startindex+i));
}
printf("\n");
return last;
} 回復 更多評論
只有注冊用戶登錄后才能發表評論。 | ||
![]() |
||
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
|
||
相關文章:
|
||