3.6字符串

5901: 【字符串】回文串

三种方法,(1)纯暴力(2)算法笔记上面的方法(3)DP

题目描述:读入一串字符,判断是否是回文串。“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。

(1)暴力

设置两个变量,左边界变量l,右边界变量r;从第一个字符和最后一个字符开始枚举对比,判断字符是否相同,额外需要注意的一点是字符串长度为奇数时枚举出口是l==r,偶数时判断l>r即可

 1 #include<iostream>
 2 #include<cstring>
 3 #include<math.h>
 4 #include<stdlib.h>
 5 #include<cstring>
 6 #include<cstdio>
 7 #include<utility>
 8 #include<algorithm>
 9 #include<map>
10 using namespace std;
11 typedef long long ll; 
12 
13 const int maxn=1005; 
14 int dp[maxn][maxn];
15 int main( )
16 {    
17     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
18     //freopen("a.txt","r",stdin);
19     //freopen("a.txt","w",stdout);
20     string s;
21     cin>>s;
22     int len=s.length();
23     
24     int l=0,r=len-1;
25     int flag=1;
26     while(1){
27         
28         if(s[l]==s[r]){
29             if(l==r||l>r)break;
30             l++;
31             r--;
32             continue;
33         }
34         else{
35             flag=0;
36             break;
37         }
38     }
39     
40     if(flag)cout<<"YES";
41     else cout<<"NO";
42     return 0;
43 }
View Code

(2)算法笔记上面的方法

即只遍历字符串前一半,设置len为字符串长度(但不需要取到i==len/2)

若出现for(int i=0;i<len;++i)s[i]!=s[len-1-i]时,则此字符串非回文串

(3)DP  后补

1001 A+B Format (20分)

题目描述:输入两个数字,相加,输出格式为每3个数字之间一个分号(从个位开始算),数字<=三位数不考虑逗号

思路:将两个数字相加后得到的整型数通过to_string函数转为字符串(必须为正数->方便,转换前预处理一下就好了),然后从个位开始判断,计数变量从0开始,%3为0时添加一个逗号,并将计数变量清零。

 1 #include<iostream>
 2 #include<sstream>
 3 #include<cstring>
 4 #include<cstdlib> 
 5 #include<cstdio> 
 6 #include<algorithm> 
 7 using namespace std;
 8 typedef long long ll;
 9 const int maxn = 1005;
10 
11 int main()
12 {
13     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
14     //freopen("a.txt","r",stdin);
15     //freopen("a.txt","w",stdout);
16     /*double n = 3.1415926;
17     string s =std::to_string(n);
18     cout << s[0]<<endl; //结果是3
19     cout << s<<endl; //结果是3.1415926*/
20 
21     /*string s1 = "2018.11";
22     int a1 = stod(s1);
23     cout << a1<<endl;
24     /*数字转字符串*/
25     int a, b;
26     cin >> a >> b;
27     int temp = a + b;
28     if (temp < 0){
29         cout << "-";temp *= -1;
30     }
31     
32 
33     string s=to_string(temp);
34     //cout << s << endl;
35     int len = s.length();
36     if (len < 4){
37         cout << s << endl;
38         return 0;
39     }
40     int cnt = 0;
41     string ans;
42     for (int i = len-1; i > -1; --i){
43 
44         if (cnt % 3 == 0&&cnt!=0){
45             ans += ",";
46             cnt = 0;
47         }
48         ans +=s[i];
49         cnt++;
50     }
51     reverse(ans.begin(),ans.end());
52     cout << ans << endl;
53     return 0;
54 }
View Code

1035 Password (20分)

题目描述:输入用户名和密码,@替换1,%替换0,L替换l,o替换O,如果所有测试样例都正确(即不需要替换),输出(题目所给的那一串,只需要注意一组正确与多组正确的区别即可)否则输出错误的样例有几个

思路:由于需要输出错误样例个数,所以需要先储存所有样例,再进行处理,以结构体数组存储,结构体成员变量为两个string,一个int变量,分别用来存储用户名与密码,以及此样例是否已修改。另外设置一个int变量来判断是否有样例修改。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<math.h>
 4 #include<stdlib.h>
 5 #include<cstring>
 6 #include<cstdio>
 7 #include<utility>
 8 #include<algorithm>
 9 #include<map>
10 using namespace std;
11 typedef long long ll; 
12 const int maxn=1005; 
13 struct node{
14     string s1,s2;
15     int flag;
16 }star[maxn];
17 int main( )
18 {    
19     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
20     //freopen("a.txt","r",stdin);
21     //freopen("a.txt","w",stdout);
22     int t;
23     int flag=1;
24     cin>>t;
25     
26     for(int j=0;j<t;++j){
27         cin>>star[j].s1>>star[j].s2;
28         int len=star[j].s2.length();
29         for(int i=0;i<len;++i){
30             
31             if(star[j].s2[i]=='1')star[j].s2[i]='@',star[j].flag=1,flag=0;
32             else if(star[j].s2[i]=='l')star[j].s2[i]='L',star[j].flag=1,flag=0;
33             else if(star[j].s2[i]=='0')star[j].s2[i]='%',star[j].flag=1,flag=0;
34             else if(star[j].s2[i]=='O')star[j].s2[i]='o',star[j].flag=1,flag=0;
35                 
36         }
37         
38     }
39     int cnt=0;
40     for(int i=0;i<t;++i){
41         if(star[i].flag==1){
42             //cout<<star[i].s1<<" "<<star[i].s2<<endl; 
43             flag=0,++cnt;
44         }
45     }
46     if(flag&&t==1){
47         cout<<"There is 1 account and no account is modified"<<endl;
48         return 0;
49     }
50     else if(flag&&t>1){
51         cout<<"There are "<<t<<" accounts and no account is modified"<<endl;
52         return 0;
53     }
54     cout<<cnt<<endl;
55     for(int i=0;i<t;++i){
56         if(star[i].flag==1){
57             cout<<star[i].s1<<" "<<star[i].s2<<endl; 
58             
59         }
60     }
61     
62     return 0;
63 }
View Code

1077 Kuchiguse (20分)

题目描述:给定多个字符串求出他们的最长公共后缀(suffix)

思路:代码中用了两种方法,第一种看起来比较乱,第二种比较简洁;

先对字符串进行预处理,我们还是习惯从第一个字符开始判断,所以reverse一下,并取一个最小字符串长度,循环中每次取出一个字符(可取任意字符串的),设为now,判断所有字符串该位置的字符是否相同,只要有一个字符串在该位置的字符与now不同则退出,若有相同,则将计数变量+1

  1 #include<iostream>
  2 #include<cstring>
  3 #include<math.h>
  4 #include<stdlib.h>
  5 #include<cstring>
  6 #include<cstdio>
  7 #include<utility>
  8 #include<algorithm>
  9 #include<map>
 10 using namespace std;
 11 typedef long long ll; 
 12 inline int read(){
 13     int X=0,w=0;char ch=0;
 14     while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
 15     while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
 16     return w?-X:X;
 17 }
 18 /*------------------------------------------------------------------------*/
 19 const int maxn=105;
 20 string s[maxn]; 
 21 int main( )
 22 {    
 23     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 24     //freopen("a.txt","r",stdin);
 25     //freopen("a.txt","w",stdout);
 26     int t;
 27     cin>>t;
 28     getline(cin,s[0]);
 29     int min_len=999; 
 30     for(int i=0;i<t;++i){
 31         
 32         getline(cin,s[i]);
 33         //cout<<s[i]<<endl;
 34         reverse(s[i].begin(),s[i].end());
 35         int len=s[i].length();
 36         min_len=min(len,min_len);
 37     }
 38     /************************/
 39     /***********方法一*************/
 40     /*string now=s[0];
 41     string ans;
 42     int flag=1; 
 43     for(int i=1;i<t;++i){
 44         string temp;
 45         if(flag==0)break;
 46         int now_flag=1;//此次是否匹配成功 
 47         
 48         for(int j=0;j<now.length();++j){
 49             if(s[i][j]==now[j]){
 50                 temp+=now[j];
 51                 ans=temp;
 52             }
 53             else if(s[i][j]!=now[j]){
 54                 if(j==0){//第一个字符匹配不上 ,说明不存在公共后缀 
 55                     flag=0;
 56                     break;
 57                 }
 58                 else{
 59                     now=ans;
 60                     break; 
 61                 } 
 62             } 
 63         }
 64         
 65     }
 66     reverse(ans.begin(),ans.end());
 67     if(flag)cout<<ans<<endl;
 68     else cout<<"nai"<<endl;*/
 69     /************************/
 70     /***********方法二*************/
 71     string ans;
 72     int cnt=0;
 73     int flag=1;
 74     for(int i=0;i<min_len;++i){
 75         if(flag==0)break;
 76         char now=s[0][i];//第一个字符 
 77         int now_flag=1;
 78         for(int j=1;j<t;++j){
 79             
 80             if(now!=s[j][i]){//若有一个字符不相同则退出 
 81                 
 82                 now_flag=0;
 83                 break;
 84             }
 85             
 86         }
 87         if(now_flag){//若所有字符串第i位相同,则计数+1 
 88             cnt++;    
 89         }
 90         else break;
 91     }
 92     //reverse(ans.begin(),ans.end());
 93     if(cnt){
 94         for(int i=0;i<cnt;++i)ans+=s[0][i];
 95         reverse(ans.begin(),ans.end());
 96         cout<<ans<<endl;
 97     }
 98     else cout<<"nai"<<endl;
 99     return 0;
100 }
View Code

1077 Kuchiguse (20分)

题目描述:

思路:

猜你喜欢

转载自www.cnblogs.com/simaomao/p/12210880.html