⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fold.java

📁 Ulm大学2003-2004年竞赛题
💻 JAVA
字号:
// Problem   Fold// Algorithm Dynamic Programming// Runtime   O(n^3)// Author    Walter Guttmann// Date      21.04.2003import java.io.*;public class fold{  static boolean av(char c1, char c2)  {    return c1=='A' && c2=='V' || c1=='V' && c2=='A';  }  public static void main(String[] arg) throws Exception  {    StreamTokenizer st = new StreamTokenizer (new FileReader ("fold.in"));    while (st.nextToken() != st.TT_EOF)    {      String s = st.sval;      int n = s.length();      int[] match = new int[n];      // match[i] is the maximal d such that s[i-d..i-1] and s[i+1..i+d]      // are reverse complementary, i.e., the 2*(d+1) stripes i-d..i      // and i+1..i+d+1 can be folded upon each other.      for (int k=0 ; k<n ; k++)        for (int i=k-1,j=k+1 ; 0<=i && j<n && av(s.charAt(i), s.charAt(j)) ; i--,j++)          ++match[k];      ++n;      int[][] dp = new int[n][n];      // dp[i][j], where i<=j, is the minimum number of folding steps      // required to produce the turns between stripes i and j,      // inclusively, i.e., those described in s[i..j-1].      for (int d=1 ; d<n ; d++)        for (int i=0,j=i+d ; j<n ; i++,j++)        {          dp[i][j] = n;          for (int k=i,l=k+1 ; k<j ; k++,l++)            if (match[k] >= Math.min(k-i, j-l))              dp[i][j] = Math.min(dp[i][j], 1+Math.max(dp[i][k], dp[l][j]));        }      System.out.println(dp[0][n-1]);    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -