• Decode the Strings


    http://acm.hdu.edu.cn/showproblem.php?pid=2371

    题意:给出一个长度为n的字符串(标号为1~n),以及n个数代表字符串的变换规则,问该字符串是由哪个字符串按照变换规则变换m次得到的?如n=5,m=3,变换规则 2 3 1 5 4 (生成的下一个字符串即按照此标号的顺序对应的串), “hello" -> "elhol" -> "lhelo" -> "helol", 故helol 是由"hello''变换3次得到的。

    思路:此类型的题目一般是找循环节,但是此题不能找字符串的循环节,因为数据范围太大,找字符串的循环节会超时。由于只有80个字符,所以可以找每个字符的循环节,并在查找过程中记录下来该字符在变换t次后所在的位置,这样就能通过m对循环节取余,找到该字符在目标串中的位置。

     1 #include <stdio.h>
     2 #include <string.h>
     3 const int N=82;
     4 int pos[N][N],p[N];
     5 char str[N],s[N];
     6 int main()
     7 {
     8     int n,m;
     9     while(~scanf("%d%d",&n,&m))
    10     {
    11         if (n==0&&m==0)
    12             break;
    13         for (int i = 0; i < n; i++)
    14         {
    15             scanf("%d%*c",&p[i]);
    16             p[i]--;
    17         }
    18         gets(s);
    19         for (int i = 0; i < n; i++)
    20         {
    21             int j = p[i];
    22             int cnt = 0;
    23             pos[i][cnt++] = i;
    24             while(j!=i)//找第i个字符的循环节
    25             {
    26                 pos[i][cnt++] = j;//表示第i个字符在变换cnt次后所在的位置
    27                 j = p[j];
    28             }
    29             int t = m%cnt;//第i个字符变换的次数
    30             str[pos[i][t]] = s[i];//将该字符存入其在目标串中的位置上
    31         }
    32         str[n] = '';
    33         puts(str);
    34     }
    35     return 0;
    36 }
    View Code
  • 相关阅读:
    ReentrantLock与Condition构造有界缓存队列与数据栈
    ReentrantLock
    文件下载
    scala初学
    bootstrap table 显示连续序号,分页有效
    web中servletcontext和applicationContext
    checkbox是否选中判断
    bookstrap form表单简单-smart-form
    charts柱状图,定时刷新
    js对象和json的区别
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3631474.html
Copyright © 2020-2023  润新知