POJ1026

 题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1026

 思路:先求每个数的循环,然后再做字符的处理。注意,如果它给的字符串不够长,要用空格代替,就是说你的输出也要有n位。还有注意每一种情况后面要输出一行空行!(为了这个PE了一次。。。郁闷)

 

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5. public class Main {
  6.     public static void main(String[] args) throws NumberFormatException,
  7.             IOException {
  8.         BufferedReader read = new BufferedReader(new InputStreamReader(
  9.                 System.in));
  10.         int len;
  11.         int[] n;
  12.         int[] loop;
  13.         String[] s;
  14.         int times;
  15.         int temp;
  16.         String str;
  17.         char[] result;
  18.         int r;
  19.         while ((len = Integer.parseInt(read.readLine())) != 0) {
  20.             n = new int[len];
  21.             loop = new int[len];
  22.             result = new char[len];
  23.             s = read.readLine().split(" ");
  24.             for (int i = 0; i < len; i++) {
  25.                 n[i] = Integer.parseInt(s[i]);
  26.             }
  27.             findLoop(n, loop);
  28.             str = read.readLine();
  29.             while ((times = Integer.parseInt(str.split(" ")[0])) != 0) {
  30.                 Arrays.fill(result, ' ');
  31.                 str = str.substring(str.indexOf(" ") + 1);
  32.                 for (int i = 0; i < str.length(); i++) {
  33.                     temp = times % loop[i];
  34.                     r = i;
  35.                     if (temp > 0) {
  36.                         r = n[i];
  37.                         for (int k = 1; k < temp; k++) {
  38.                             r = n[r - 1];
  39.                         }
  40.                         result[r - 1] = str.charAt(i);
  41.                     } else {
  42.                         result[r] = str.charAt(i);
  43.                     }
  44.                 }
  45.                 System.out.println(String.valueOf(result));
  46.                 str = read.readLine();
  47.             }
  48.             System.out.println();
  49.         }
  50.     }
  51.     public static void findLoop(int[] n, int[] loop) {
  52.         int temp;
  53.         int c;
  54.         for (int i = 0; i < n.length; i++) {
  55.             temp = 1;
  56.             c = n[n[i] - 1];
  57.             while (n[i] != c) {
  58.                 c = n[c - 1];
  59.                 temp++;
  60.             }
  61.             loop[i] = temp;
  62.         }
  63.     }
  64. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值