zoukankan      html  css  js  c++  java
  • Round #449(Div 2) 回文预习+复习

    A. Scarborough Fair
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output
    Are you going to Scarborough Fair?

    Parsley, sage, rosemary and thyme.

    Remember me to one who lives there.

    He once was the true love of mine.

    Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there.

    Willem asks his friend, Grick for directions, Grick helped them, and gave them a task.

    Although the girl wants to help, Willem insists on doing it by himself.

    Grick gave Willem a string of length n.

    Willem needs to do m operations, each operation has four parameters l, r, c1, c2, which means that all symbols c1 in range [l, r] (from l-th to r-th, including l and r) are changed into c2. String is 1-indexed.

    Grick wants to know the final string after all the m operations.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 100).

    The second line contains a string s of length n, consisting of lowercase English letters.

    Each of the next m lines contains four parameters l, r, c1, c2 (1 ≤ l ≤ r ≤ nc1, c2 are lowercase English letters), separated by space.

    Output

    Output string s after performing m operations described above.

    Examples
    input
    3 1
    ioi
    1 1 i n
    output
    noi
    input
    5 3
    wxhak
    3 3 h x
    1 5 x a
    1 3 w g
    output
    gaaak
    Note

    For the second example:

    After the first operation, the string is wxxak.

    After the second operation, the string is waaak.

    After the third operation, the string is gaaak.

     数据量小,所以暴力就好,注意  字符串是以0开始的,,其他就是模拟题目去替换

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 #define lowbit(x) (x&(-x))
     4 #define max(x,y) (x>=y?x:y)
     5 #define min(x,y) (x<=y?x:y)
     6 #define MAX 100000000000000000
     7 #define MOD 1000000007
     8 #define pi acos(-1.0)
     9 #define ei exp(1)
    10 #define PI 3.1415926535897932384626433832
    11 typedef long long ll;
    12 #define INF 0x3f3f3f3f
    13 #define mem(a) memset(a,0,sizeof(a))
    14 const int maxn=100030;
    15 ll a[maxn];
    16 int main(){
    17     ios::sync_with_stdio(0);
    18     int n,m;
    19     string s;
    20     cin>>n>>m;
    21     cin>>s;
    22     while(m--){
    23         int l,r;
    24         char c1,c2;
    25         cin>>l>>r;
    26         cin>>c1>>c2;
    27         for(int i=l-1;i<=r-1;i++){
    28             if(c1==s[i]){
    29                 s[i]=c2;
    30             }
    31         }
    32     }
    33     cout<<s;
    34     return 0;
    35 }
    B. Chtholly's request
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output
    — Thanks a lot for today.

    — I experienced so many great things.

    — You gave me memories like dreams... But I have to leave now...

    — One last request, can you...

    — Help me solve a Codeforces problem?

    — ......

    — What?

    Chtholly has been thinking about a problem for days:

    If a number is palindrome and length of its decimal representation without leading zeros is even, we call it a zcy number. A number is palindrome means when written in decimal representation, it contains no leading zeros and reads the same forwards and backwards. For example 12321 and 1221 are palindromes and 123 and 12451 are not. Moreover, 1221 is zcy number and 12321 is not.

    Given integers k and p, calculate the sum of the k smallest zcy numbers and output this sum modulo p.

    Unfortunately, Willem isn't good at solving this kind of problems, so he asks you for help!

    Input

    The first line contains two integers k and p (1 ≤ k ≤ 105, 1 ≤ p ≤ 109).

    Output

    Output single integer — answer to the problem.

    Examples
    input
    2 100
    output
    33
    input
    5 30
    output
    15
    Note

    In the first example, the smallest zcy number is 11, and the second smallest zcy number is 22.

    In the second example, .

     要求k个回文数(偶数位数)相加mod p,,注意ans=n,不是0,

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 #define  ll long long
     4 int k;
     5 ll p;
     6 ll sum=0;
     7  ll palindrome(int n){
     8      ll ans=n,t=n;
     9      while(t){
    10          ans=ans*10+t%10;
    11          t/=10;
    12      }
    13      return ans;
    14  }
    15 int main(){
    16     ios::sync_with_stdio(false);
    17     cin>>k>>p;
    18     for(int i=1;i<=k;i++){
    19         sum=(sum+palindrome(i))%p;
    20     }
    21     cout<<sum%p<<endl;
    22 }

    回文数

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:0
     
    描述
    请寻找并输出1至1000000之间的数m,它满足m、m^2和m^3均为回文数。回文数大家都知道吧,就是各位数字左右对称的整数,例如121、676、123321等。满足上述条件的数如m=11,m^2=121,m^3=1331皆为回文数。
     
    输入
    没有输入
    输出

    输出1至1000000之间满足要求的全部回文数,每两个数之间用空格隔开,每行输出五个数

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 bool palindrom(ll n){
     5     ll ans=0,t=n;
     6     while(t){
     7         ans=ans*10+t%10;
     8         t/=10;
     9     }
    10     if(ans==n){
    11         return 1;
    12     }else{
    13         return 0;
    14     }
    15 }
    16 int main(){
    17     ios::sync_with_stdio(0);
    18     ll a,b;
    19     int count=0;
    20     for(ll i=1;i<=1000000;i++){
    21         a=i*i;
    22         b=a*i;
    23         if(palindrom(i)&&palindrom(a)&&palindrom(b)){
    24             count++;
    25             cout<<i;
    26             if(count<5){
    27                 cout<<" ";
    28             }else{
    29                 count=0;
    30                 cout<<endl;
    31             }
    32         }
    33     }
    34     cout<<endl;
    35         return 0;
    36 }

    少年 DXH

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:2
     
    描述
    大家都知道,DXH 幼时性格怪癖,小朋友都不喜欢和他玩,这种情况一直到 DXH 的少年时期也没有改变。
    少年时期的 DXH 迷上了"回文串",“回文串”是指正着读倒着读都一样的字符串。DXH一个人的时候喜欢在地上随便写一个字符串 S,然后在这个字符串的右边添加尽量少的字符(可以不添加,就是 0 个),使得这个字符串变成“回文串”。但玩的时间长了,DXH 的手也酸了,聪明的你能帮他写一个程序,算出这个添加最少字符形成的回文串吗?
    一个字符串 S[1..L]被称为回文串,当且仅当 S[i] = S[L - i + 1] (1 <= i <= L)。
     
    输入
    第一行,一个 T (T <= 100),表示有 T 个字符串需要你判断
    之后 T 行,每行一个字符串 S。
    S 的长度|S|满足 1 <= |S| <= 50,且 S 只由小写字母'a' -'z'组成。
    输出
    对于每个字符串,输出一行,每行一个字符串 S', 是在 S 右侧添加最少的字符形成的回文串。
    样例输入
    5
    add
    cigartragic
    dxhisgirl
    acaba
    abczyxyz
    样例输出
    adda
    cigartragic
    dxhisgirlrigsihxd
    acabaca
    abczyxyzcba
    来源
    山东大学 ACM/ICPC 校赛高年级组试题 2012
    一开始理解错题意,额,,回文串,首先,它本身就满足题意,如若不满足,那即是在后面添加尽量少的元素,暴力枚举
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 int  palindrom1(string s){
     5     int i=0,j=s.length()-1;
     6     while(i!=j){   //i ,j,,思考   错的函数,理所当然的幻想
     7         if(s[i]==s[j]){
     8             ++i;
     9             --j;
    10         }else{
    11             return 0;
    12         }
    13     }
    14     return 1;
    15 }
    16 int palindrom(string s){
    17     int i;
    18     int j=s.length()-1;
    19     for(i=0;i<j;++i,--j){
    20         if(s[i]!=s[j]){
    21             return 0;
    22         }
    23     }
    24     return 1;
    25 }
    26 
    27 int main(){
    28     ios::sync_with_stdio(0);
    29     int t;
    30     cin>>t;
    31     while(t--){
    32         string s,ss;
    33         cin>>s;
    34         if(palindrom(s)){
    35             cout<<s<<endl;
    36         }
    37         else{
    38             for(int i=0;i<s.length();++i){
    39                     ss=s;
    40                     for(int j=i;j>=0;--j)
    41                         ss+=s[j];   //jjjjjjjjjjj,手贱
    42 
    43                     if(palindrom(ss)){
    44                         cout<<ss<<endl;
    45                     break;  //break  break跳出循环
    46                     }
    47             }
    48         }
    49     }
    50         return 0;
    51 }

    最长回文子串

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:4
     
    描述
    输入一个字符串,求出其中最长的回文子串。子串的含义是:在原串连续出现的字符串片段。回文的含义是:正着看和倒着看是相同的,如abba和abbebba。在判断是要求忽略所有的标点和空格,且忽略大小写,但输出时按原样输出(首尾不要输出多余的字符串)。输入字符串长度大于等于1小于等于5000,且单独占一行(如果有多组答案,输出第一组)。
     
    输入
    输入一个测试数据n(1<=n<=10);
    随后有n行,每行有一个字符串。
    输出
    输出所要求的回文子串。
    样例输入
    1
    Confuciuss say:Madam,I'm Adam.
    样例输出
    Madam,I'm Adam

    /*
    细节决定最终的成败

    题解:因为最终要按照原样输出,而开始判断市又不区分大小写,所以一开始要开辟一个数组来存储最原始的字符串样子;
    然后把所有的字母要不变成大写,要不就是小写,以便于后面比较求取最长回文串
    回文串按长度可分奇偶,所以先确定一个中心轴,根据奇偶特性向两边扩展,分别求取在这两种情况下的找相对长的回文串,不断更新这个长度
    当全部遍历完后,既是最长回文串啦
    */

     1 #include <iostream>
     2 #include <string.h>
     3 #include <ctype.h>
     4 #include <stdio.h>
     5 using namespace std;
     6 const int maxn=1e5+10;
     7 char p[maxn];
     8 char tempStr[maxn];
     9 int main(){
    10     //ios::sync_with_stdio(0);
    11 #ifdef LOCAL
    12     freopen("input","r",stdin);
    13     freopen("output","w",stdout);
    14 #endif
    15     int n;
    16     cin>>n;
    17     getchar();
    18     while(n--){
    19         string str;
    20         getline(cin,str);
    21         int m=0;
    22         for(int i=0;i<str.length();++i){
    23             if(isalpha(str[i])){
    24                 p[m]=i;
    25                 tempStr[m++]=toupper(str[i]);
    26             }
    27         }
    28         int maxx=0;
    29         int x,y;
    30         for(int i=0;i<m;i++){
    31             for(int j=0;j<=i&&i+j<m;++j){
    32                 if(tempStr[i-j]!=tempStr[i+j])
    33                     break;
    34                 if(j*2+1>maxx){
    35                     maxx=j*2+1;
    36                     x=p[i-j];
    37                     y=p[i+j];
    38                 }
    39             }
    40             for(int j=0;j<=i&&i+j+1<m;++j){
    41                 if(tempStr[i-j]!=tempStr[i+j+1])
    42                     break;
    43                 if(j*2+2>maxx){
    44                     maxx=j*2+2;
    45                     x=p[i-j];
    46                     y=p[i+j+1];
    47                 }
    48             }
    49     }
    50     for(int i=x;i<=y;++i)
    51         cout<<str[i];
    52     cout<<endl;
    53     }
    54     return 0;
    55 }


  • 相关阅读:
    Intent
    BroadcastReceiver
    AsyncTask两种线程池
    多线程、Service与IntentService的比较
    AsyncTask
    转:Android开发:使用DDMS Heap进行内存泄露调试
    (原创)Android Binder设计与实现
    (原)一句mpAudioPolicy->get_input引发的血案
    LOCAL_WHOLE_STATIC_LIBRARIES与LOCAL_STATIC_LIBRARIES的区别
    非static成员函数通过类名::来调用,空指针调用成员方法不出错!
  • 原文地址:https://www.cnblogs.com/z-712/p/7965337.html
Copyright © 2011-2022 走看看