Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

提交了几次,找不出WA原因。大牛 帮看下?

Posted by ncptbtptp at 2013-04-08 20:27:24 on Problem 1226
#include <stdio.h>
#include <string.h>

const unsigned int kBufferSize = 105;
char g_shortest[kBufferSize];
char g_lines[100][210];

void reverse(char dest[], char source[])
{
   unsigned int length = strlen(source);
   for (int i = 0; i < length; ++i)
   {
      dest[i] = source[length - 1 - i];
   }
   dest[length] = '\0';
}

// lineNo starts from zero.
void scanLine(int lineNo)
{
   char line[kBufferSize];
   scanf("%s", line);

   if ((strlen(g_shortest) == 0) || (strlen(line) < strlen(g_shortest)))
   {
      strcpy(g_shortest, line);
   }

   strcpy(g_lines[lineNo], line);
   // Concatenate the reverse.
   reverse(g_lines[lineNo] + strlen(line) - 1, line);
}

int getCommonLength(int lineNum)
{
   int len = strlen(g_shortest);
   while (len > 0)
   {
      // Enum all substrings of length "len" in "shortest",
      // and test if that's contained by other strings.
      //for (int i = 0; i < strlen(g_shortest); i += len
      for (int i = 0; len <= strlen(g_shortest) - i; ++i)
      {
         char tmp = g_shortest[i + len];
         // Mark the new(temp) end.
         g_shortest[i + len] = '\0';

         // Search all strings to see if this is a substring.
         int j;
         for (j = 0; j < lineNum; ++j)
         {
            if (!strstr(g_lines[j], g_shortest + i))
            {
               break;
            }
         }

         // Restore the old.
         g_shortest[i + len] = tmp;

         if (j == lineNum)
         {
            return len;
         }
      }//for

      --len;
   }//while

   // No common string found.
   return 0;
}

int testCase()
{
   g_shortest[0] = '\0';
   int lineNum = 0;
   scanf("%d", &lineNum);
   for (int i = 0; i < lineNum; ++i)
   {
      scanLine(i);
   }

   return getCommonLength(lineNum);
}

int main()
{
   int tests;
   scanf("%d", &tests);

   for (int i = 0; i < tests; ++i)
   {
      printf("%d\n", testCase());
   }

   return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator