zoukankan      html  css  js  c++  java
  • Codeforces Round #265 (Div. 1)

    A No to Palindromes!

    题意:给一个长度为n的用前m个字符构成的字符串,定义一个字符串是好的当且仅当他的每个子串都不是回文的,现在给出一个好的字符串,求比他字典序大的第一个好的字符串。

    题解:从后往前每一位枚举,若把当前枚举的位改成ch后为好的串,只需要判断他和他前面的一个字符是否相同构成长度为2的回文串,或者和他前面的前面的两个字符构成长度为3的回文串。

    于是找到第一个可以换的位置,后面每个位置从'a'开始枚举可以取得的第一个字符即可。

     1 #include <cstdio>
     2 
     3 #include <cmath>
     4 
     5 #include <cstring>
     6 
     7 #include <iostream>
     8 
     9 #include <algorithm>
    10 
    11 
    12 
    13 using namespace std;
    14 
    15 char s[2000];
    16 
    17 int n,p;
    18 
    19 
    20 
    21 int check(int pos,char ch)
    22 
    23 {
    24 
    25     if (pos>0&&s[pos-1]==ch) return 0;
    26 
    27     if (pos>1&&s[pos-2]==ch) return 0;
    28 
    29     return 1;
    30 
    31 }
    32 
    33 int main()
    34 
    35 {
    36 
    37     scanf("%d %d",&n,&p);
    38 
    39     scanf("%s",s);
    40 
    41     for (int i=n-1;i>=0;--i)
    42 
    43     {
    44 
    45         for (int ch=s[i]+1;ch<'a'+p;++ch)
    46 
    47         {
    48 
    49             if (!check(i,ch)) continue;
    50 
    51             s[i]=ch;
    52 
    53             for (int j=i+1;j<n;++j) 
    54 
    55                 for (s[j]='a';!check(j,s[j])&&s[j]<'a'+p;++s[j]);
    56 
    57             printf("%s",s);
    58 
    59             return 0;
    60 
    61         }
    62 
    63     }
    64 
    65     printf("NO");
    66 
    67     return 0;
    68 
    69 }
    View Code

    B Restore Cube

    题意:给出八个三元组(x,y,z),每个三元组内元素可相互交换,问在交换后有没有可能这八个三元组对应在空间的八个点构成一个正方体。

    题解:明显直接枚举所有情况检查是否可行即可,对于检查八个点能否构成一个正方体。。我们可以枚举每个点然后算出其他点到他的距离

    如果距离均为x x x 根号二x 根号二x 根号二x 根号三x这样的形式则必为一个正方体。

      1 #include <cstdio>
      2 
      3 #include <iostream>
      4 
      5 #include <cmath>
      6 
      7 #include <cstring>
      8 
      9 #include <algorithm>
     10 
     11 using namespace std;
     12 
     13 typedef long long LL;
     14 
     15 LL map[10][5];
     16 
     17 LL a[10];
     18 
     19 
     20 
     21 LL dist(int x,int y)
     22 
     23 {
     24 
     25     return (map[x][1]-map[y][1])*(map[x][1]-map[y][1])+(map[x][2]-map[y][2])*(map[x][2]-map[y][2])+(map[x][3]-map[y][3])*(map[x][3]-map[y][3]);
     26 
     27 }
     28 
     29 int work(int x)
     30 
     31 {
     32 
     33     int num=0;
     34 
     35     for (int i=1;i<=8;++i)
     36 
     37     {
     38 
     39         if (i==x) continue;
     40 
     41         ++num;a[num]=dist(i,x);
     42 
     43     }
     44 
     45     sort(a+1,a+1+num);
     46 
     47     if (a[1]==0||a[1]!=a[2]||a[1]!=a[3]||a[4]!=a[5]||a[4]!=a[6]||a[4]!=2*a[1]||a[7]!=3*a[1]) return 0;
     48 
     49     return 1;
     50 
     51 }
     52 
     53 int check()
     54 
     55 {
     56 
     57     for (int i=1;i<=8;++i)    if (!work(i)) return 0;
     58 
     59     printf("YES
    ");
     60 
     61     for (int i=1;i<=8;++i)
     62 
     63     {
     64 
     65         for (int j=1;j<=3;++j) printf("%I64d ",map[i][j]);
     66 
     67         printf("
    ");
     68 
     69         
     70 
     71     }
     72 
     73     return 1;
     74 
     75 }
     76 
     77 int dfs(int x)
     78 
     79 {
     80 
     81     if (x==9) return check();
     82 
     83     sort(map[x]+1,map[x]+4);
     84 
     85     do
     86 
     87     {
     88 
     89         if (dfs(x+1)) return 1;;
     90 
     91     }while (next_permutation(map[x]+1,map[x]+4));
     92 
     93     return 0;
     94 
     95 }
     96 
     97 int main()
     98 
     99 {
    100 
    101     for (int i=1;i<=8;++i) 
    102 
    103         for (int j=1;j<=3;++j) scanf("%I64d",&map[i][j]);
    104 
    105     if (!dfs(1)) printf("NO
    ");
    106 
    107     return 0;
    108 
    109 }
    View Code

    C Substitutes in Number

    题意:给出一个数字,现在给出很多个替换,对于某个替换:要求把从0到9的某个数字全部换成数字T(可为空),求最后变换后的数mod(1e9+7)。

    题解:假设我们最后得到了每个数字对应的数,那么我们就可以从初始的数字直接转化到最后的结果。

       于是我们考虑如何得到每个数字最后对应的数,首先我们知道肯定应该倒过来考虑整个操作序列,因为后面的操作是会影响到前面的操作的。

       于是我们从最后一个操作开始每一步求出数字当前对应的数是多少,对于变换成的数字T我们也要按照现在已有的对应法则变换过去,其实就是模拟一下还原的过程。

       为了操作方便记录一下数字对应的数的位数更方便还原,做到这里应该就解决了问题。

      

     1 #include <cstdio>
     2 
     3 #include <cmath>
     4 
     5 #include <cstring>
     6 
     7 #include <iostream>
     8 
     9 #include <algorithm>
    10 
    11 
    12 
    13 typedef long long LL;
    14 
    15 
    16 
    17 using namespace std;
    18 
    19 const int mod=1e9+7;
    20 
    21 string s,q[200000];
    22 
    23 LL add[20],shift[20];
    24 
    25 int n;
    26 
    27 int main()
    28 
    29 {
    30 
    31     cin>>s;
    32 
    33     cin>>n;
    34 
    35     for (int i=0;i<10;++i)
    36 
    37     {
    38 
    39         add[i]=i;
    40 
    41         shift[i]=10;
    42 
    43     }
    44 
    45     for (int i=0;i<n;++i) cin>>q[i];
    46 
    47     for (int i=n-1;i>=0;--i)
    48 
    49     {
    50 
    51         LL dqa=0,dqs=1;
    52 
    53         for (int j=q[i].size()-1;j>=3;--j)
    54 
    55         {
    56 
    57             dqa=(dqa+add[q[i][j]-'0']*dqs)%mod;
    58 
    59             dqs=(shift[q[i][j]-'0']*dqs)%mod;
    60 
    61         }
    62 
    63         int x=q[i][0]-'0';
    64 
    65         add[x]=dqa;
    66 
    67         shift[x]=dqs;
    68 
    69     }
    70 
    71     LL ans=0;
    72 
    73     for (int i=0;i<s.size();++i) ans=(ans*shift[s[i]-'0']+add[s[i]-'0'])%mod;
    74 
    75     cout<<ans<<endl;
    76 
    77     return 0;
    78 
    79 }
    View Code
  • 相关阅读:
    NowCoder数列(矩阵快速幂变式)
    P1147 连续自然数和(思维题)
    P1029 最大公约数和最小公倍数问题(思维题)
    Diophantus of Alexandria
    欧拉筛法求素数( 洛谷)
    欧拉函数
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/zscnash/p/4156530.html
Copyright © 2011-2022 走看看