字符串类型
1.B - Beautiful Strings (atcoder.jp)
代码:

1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int n; 5 int cnt[500]; 6 string s; 7 int mian() { 8 cin>>s; 9 n=s.size(); 10 s=' '+s; 11 12 for(int i=1; i<=n; i++) { 13 cnt[s[i]]++; 14 } 15 16 int ok=1; 17 for(int i='a'; i<='z'; i++) { 18 if(cnt[i]%2==1) ok=0; 19 } 20 if(ok) cout<<"Yes" ; 21 else cout<<"No"; 22 }

1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 6 7 int main() 8 { 9 string s; 10 cin >> s; 11 int i, j; 12 13 for (i = 0; i < s.length(); i++) 14 { 15 for (j = i+1; j < s.length(); j++) 16 { 17 if (s[j] == s[i]) 18 { 19 printf("no"); 20 return 0; 21 } 22 } 23 } 24 25 printf ("yes"); 26 return 0; 27 }

1 #include <stdio.h> 2 #include <string.h> 3 int main(){ 4 char a[26]; 5 int b[26]={0}; 6 scanf("%s",&a); 7 int n; 8 n=strlen(a); 9 for(int i=0;i<n;i++){ 10 b[a[i]-97]++; 11 } 12 int flag=1; 13 for(int i=0;i<n;i++){ 14 if(b[i]>1){ 15 flag=0; 16 } 17 } 18 if(flag==0){ 19 printf("no"); 20 } 21 else{ 22 printf("yes"); 23 } 24 return 0; 25 }
byd第三个代码就是过不了

1 #include <bits/stdc++.h> 2 using namespace std; 3 4 string a; 5 int s,cnt[1000]; 6 int main() 7 { 8 cin >> a; 9 s = a.size(); 10 11 for(int i = 0; i < s ; i ++) 12 { 13 cnt[a[i]] ++; 14 } 15 16 int ok = 1; 17 for(int i = 0; i < n; i ++) 18 { 19 if(cnt[i] > 1) ok = 0; 20 } 21 22 if(ok) cout << "Yes"; 23 else cout << "No"; 24 return 0 ; 25 }
3.Increment Decrement - AtCoder abc052_b - Virtual Judge (vjudge.net)

1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int n,cnt,ma; 5 char a[1000]; 6 7 8 int main() 9 { 10 cin >> n; 11 12 for (int i = 1; i <= n; i ++ ) 13 { 14 cin >> a[i]; 15 if(a[i] == 'D') cnt --; 16 else cnt ++; 17 if(cnt > ma) ma = cnt; 18 } 19 cout << ma; 20 return 0; 21 }
4.字符串的增删Unhappy Hacking (ABC Edit) - AtCoder abc043_b - Virtual Judge (vjudge.net)
for(char x: s) 用pop_back()和push_back()

1 #include <bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 ios::sync_with_stdio(false); 5 cin.tie(0); 6 7 string s,tmp=""; 8 cin>>s; 9 10 for(char x:s){ 11 if(x=='B'&&!tmp.empty()){ 12 tmp.pop_back(); 13 } 14 else if(x!='B'){ 15 tmp.push_back(x); 16 } 17 } 18 cout<<tmp<<"\n"; 19 return 0; 20 }
5.Two Anagrams - AtCoder abc082_b - Virtual Judge (vjudge.net)
字符串的排序和逆转

1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() { 4 string s,t; 5 6 cin>>s; 7 cin>>t; 8 9 sort(s.begin(),s.end()); 10 sort(t.begin(),t.end()); 11 reverse(t.begin(),t.end()); 12 13 if(s<t) 14 cout<<"Yes"; 15 else 16 cout<<"No"; 17 return 0; 18 }
6.Grid Compression - AtCoder abc107_b - Virtual Judge (vjudge.net)
字符串组 的整排,整列的增删,如果一排都相等,则重新定义一个数组去进行判断

1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int n,m; 5 char s[101][101]; 6 int l[101],r[101]; 7 8 int main() 9 { 10 cin >> n >> m; 11 for (int i = 1; i <= n; i ++ ) 12 for (int j = 1; j <= m; j ++ ) cin >>s[i][j]; 13 14 for (int i = 1; i <= n; i++){ 15 int t = 0; 16 for (int j = 1; j <= m; j++){ 17 if(s[i][j] == '.'){ 18 t++; 19 } 20 } 21 if(t == m) l[i] = 1; 22 } 23 for (int i = 1; i <= m; i++){ 24 int t = 0; 25 for (int j = 1; j <= n; j++){ 26 if(s[j][i] == '.'){ 27 t++; 28 } 29 } 30 if(t == n) r[i] = 1; 31 } 32 for (int i = 1; i <= n; i++){ 33 int flag = 0; 34 for (int j = 1; j <= m; j++){ 35 if(l[i] || r[j]) continue; 36 flag = 1; 37 printf("%c", s[i][j]); 38 } 39 if(flag) printf("\n"); 40 } 41 42 return 0; 43 }
7.字符串组的增删Snuke's Coloring 2 (ABC Edit) - AtCoder abc047_b - Virtual Judge (vjudge.net)

1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 5 int main(){ 6 ll n,m,q; 7 cin >> m >> n >> q; 8 9 ll s[n+5][m+5]; 10 for(ll i = 1;i <= n;i++){ 11 for(ll j = 1;j <= m;j++){ 12 s[i][j] = 1; 13 } 14 } 15 while(q--){ 16 ll x,y,a; 17 cin>>x>>y>>a; 18 if(a == 1){ 19 for(ll i=1;i<=n;i++){ 20 for(ll j=1;j<=x;j++) s[i][j]=0; 21 } 22 } 23 if(a==2){ 24 for(ll i=1;i<=n;i++){ 25 for(ll j=x+1;j<=m;j++) s[i][j]=0; 26 } 27 } 28 if(a==3){ 29 for(ll i=n-y+1;i<=n;i++){ 30 for(ll j=1;j<=m;j++) s[i][j]=0; 31 } 32 } 33 if(a==4){ 34 for(ll i=1;i<=n-y;i++){ 35 for(ll j=1;j<=m;j++) s[i][j]=0; 36 } 37 } 38 } 39 ll res=0; 40 for(ll i=1;i<=n;i++){ 41 for(ll j=1;j<=m;j++){ 42 if(s[i][j]) res++; 43 } 44 } 45 cout<<res; 46 }
8.表示第一次出现和最后一次出现的新颖的方法,以及找规律
找到第一个A出现的位置和最后一次B出现的位置

1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int N = 2e5 + 10; 5 int n,t; 6 char s[N]; 7 8 int main() 9 { 10 cin >> t; 11 while(t -- ) 12 { 13 scanf("%d%s", &n, s + 1); 14 15 int ans = 0,x = 0,y = 0; 16 for (int i = 1; i <= n; i ++ ) 17 { 18 if(s[i] == 'B') y = i; 19 else { 20 if(x == 0) x = i; 21 } 22 } 23 if(x!=0&&y!=0){ 24 if(y >= x) ans = y - x; 25 } 26 cout << ans << endl; 27 } 28 }
9.又又又是字符串的排序和逆转 Problem - C - Codeforces
这个题要求找到最长的非上升字典序进行循环排序,找到最少操作次数,我们可以把按照要求的字符下标丢到vetor里面然后swap转换,最后再判断一下是否有序

1 #include<bits/stdc++.h> 2 using namespace std; 3 #define endl '\n' 4 #define int long long 5 #define cy cout << "YES" << endl 6 #define cn cout << "NO" << endl 7 #define c1 cout << -1 << endl 8 int _,n,m,cnt; 9 const int N = 4e3 + 10,inf = 1e9; 10 const int mod = 1e9 + 7; 11 12 signed main() 13 { 14 cin >> _; 15 while(_ -- ){ 16 int n; 17 cin >> n; 18 string s; 19 cin >> s; 20 vector<int> a; 21 for (int i = n - 1; i >= 0; i -- ) 22 if(a.empty() || s[i] >= s[a.back()]) //如果后一位字典序小于等于前一位 23 a.push_back(i); //存入最大子序列下标 24 25 reverse(a.begin(),a.end()); 26 int ans = 0; 27 int j = 0; 28 while(j < a.size() && s[a[0]] == s[a[j]]) j += 1; 29 ans = a.size() - j; 30 31 for (int i = 0; i < a.size() - 1 - i; i ++ ){ 32 swap(s[a[i]],s[a[a.size() - 1 - i]]); //循环子序列 33 } 34 35 if(!is_sorted(s.begin(),s.end())) ans = -1;//判断循环后的序列是否有序 36 cout << ans << endl; 37 } 38 return 0; 39 }