PAT甲级1084 Broken Keyboard:[C++题解]字符串处理、双指针算法
生活随笔
收集整理的這篇文章主要介紹了
PAT甲级1084 Broken Keyboard:[C++题解]字符串处理、双指针算法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 題目分析
- 題目來源
題目分析
來源:acwing
ac代碼1:雙指針?biāo)惴?/p>
讀入兩個字符串a(chǎn)和b,用兩個指針i和j分別遍歷a、b。如果a[i] ==b[j],說明鍵盤沒壞,j往后移動一個;如果a[i] != b[j],說明a[i]這個鍵盤壞了。
注意:一個小寫變大寫的函數(shù)toupper
#include<bits/stdc++.h> using namespace std;int main(){string a, b;cin >> a >> b;b+='#'; //防止出現(xiàn)a長,b短的情況,b末尾加一個未出現(xiàn)的字符,防止b越界bool st[200] ={0};//標(biāo)記是否輸出過for(int i = 0, j = 0; i<a.size(); i++){char x = toupper(a[i]), y = toupper(b[j]);if(x == y) j++;else{if(!st[x]) cout << x ,st[x] = true;}}}ac代碼2:使用find函數(shù)復(fù)雜度高
#include<bits/stdc++.h> using namespace std;int main(){string a, b;cin >> a >> b;string res;for(auto ch: a)if(b.find(ch) ==-1){if(ch>='a'&&ch<='z'){ch = toupper(ch);//大寫}if(res.find(ch)==-1) res+= ch;}cout<<res;}題目來源
https://www.acwing.com/problem/content/1572/
PAT甲級1084 Broken Keyboard
總結(jié)
以上是生活随笔為你收集整理的PAT甲级1084 Broken Keyboard:[C++题解]字符串处理、双指针算法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT甲级1082 Read Numbe
- 下一篇: PAT甲级1108 Finding Av