3.6 String

5901: palindromic sequence [String]

Three methods, (1) a pure force (2) Algorithm notes above method (3) DP

Description Title: reading a string of characters, whether or not palindromic sequence. "Palindrome string" is a positive reading and verlan same string, such as "level" or "noon" and so is a palindrome string.

(1) violence

Set two variables, the variables L left boundary, a right boundary variables R & lt; a character from the first and last character begins the enumeration of comparison, it is determined whether or not the same character, an additional point to note is the length of the string is odd enumeration outlet l == r, determining l> r can be an even number

 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) The above method algorithm notes

That is, only half of the traversal string, the string length len is set (but do not need to take i == len / 2)

If there is for (int i = 0; i <len; ++ i)! S [i] = time s [len-1-i], the non-palindromic sequence string

(3) DP complement the

 

1001 A + B Format (20 minutes)

Description Title: Enter two digits, adding the output format is a semicolon between every three digits (counting from the start bit), digital <= not consider a three-digit comma

Ideas: The two integer numbers obtained after addition into a string (must be positive -> convenient, look like a pretreatment before conversion) by to_string function, and then determines the start bit, the counting variable 0, 3% add a comma is 0, and the counter variable is cleared.

 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 minutes)

Description Title: user name and password, replacement @ 1, replacing% 0, L replacing l, o Alternatively O, provided all the test samples are correct (i.e., need not be replaced), the output (Subject given that string, only Note the correct set of multiple sets of difference can be correct) or output error of a few examples

Ideas: the need to sample the output of the number of errors, it is necessary to store all the samples, and then processed to memory array of structures, the structures of two member variables string, int a variable, it is used to store user name and password and this sample has been modified. Int additionally provided a variable to determine whether to modify the sample.

 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 minutes)

Topic Description: The given more strings to obtain their longest common suffix (suffix)

Ideas: the code by the two methods, the first looks messy, a second relatively simple;

First string pretreatment, we start determination habits from the first character, the reverse bit, and takes a minimum string length, the loop takes out a character (string take any), to now, Analyzing all the characters of the string that is the same position, as long as a different character in the string is now the exit position, if the same, then the variable count +

  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 is      / * * ********************* * / 
39      / * ********** method ************ * / 
40      / * String now = S [0];
 41 is      String ANS;
 42 is      int In Flag =. 1; 
 43 is      for (int I =. 1; I <T; I ++) {
 44 is          String TEMP;
 45          IF (In Flag == 0) BREAK;
 46 is          int = now_flag. 1; // if the matching is successful 
 47          
48         for (int J = 0; J <now.length (); J ++) {
 49              IF (S [I] [J] == now [J]) {
 50                  TEMP = now + [J];
 51 is                  ANS = TEMP ;
 52 is              }
 53 is              the else IF (! S [I] [J] = now [J]) {
 54 is                  IF (J == 0) {// the first character is not a match, indicating the absence of a common suffix 
 55                      In Flag = 0 ;
 56 is                      BREAK;
 57 is                  }
 58                  the else {
 59                      now = ANS;
 60                      BREAK; 
 61 is                  } 
 62 is              } 
 63 is         }
 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]; // the first character 
77          int now_flag = . 1 ;
 78          for ( int J = . 1 ; J <T; ++ J) {
 79              
80              IF (now =! S [J] [i]) { // if a character is not the same as the exit 
81                  
82                  now_flag = 0 ;
 83                  BREAK ;
 84              }
 85              
86          }
 87          IF (now_flag) { // If all bits of the same i-th string, the count + 
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 minutes)

Subject description:

Ideas:

Guess you like

Origin www.cnblogs.com/simaomao/p/12210880.html