hdu 1238
找出字符串中,连续子串(逆子串)相等的长度
简单字符串处理

首先要读懂题意,题目给出一串手机号,按格式从大到小输出手机号中含有的数字,存入arr数组,再输出每个手机号的数字在arr数组中的下标。
处理的几个巧妙之处:
- 首先把vis数组初始化为1(默认手机号的每个数字都已经访问过),然后通过循环查询,出现手机号码中的数字则记录,没有出现则通过判断语句筛掉了,实现找出没有重复的数字。
 
- 数字间隔“,”的输出巧妙,输出的第一个肯定不是”,”,排除掉之后间隔输出”,”和数组中的数字。
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
   | #include <iostream> #include <cstring> #include <algorithm> using namespace std; int arr[100];   int vis[100];  int a[100];   bool cmp(int a,int b) {     return a>b; }
  int main() {     string s;     int k=0;     cin >> s;     int len = s.size();
           for(int i=0;i<len;i++)         vis[s[i]-'0'] = 1;
      for(int i=0;i<len;i++)     {         if(vis[i])             arr[k++] = i;        }
      cout << "int[] arr = new int[]{";      sort(arr,arr+k,cmp);     for(int i=0;i<k;i++)       {         if(i != 0 )             cout << ",";         cout << arr[i];     }     cout << "};" <<endl;
 
 
      cout << "int[] index = new int[]{";     for(int i=0;i<len;i++)     {         for(int j=0;j<k;j++)         {             if((s[i]- '0') == arr[j])             {                 a[i] = j;             }         }     }
      for(int i=0;i<len;i++)     {         if(i!=0)                       cout << ",";         cout << a[i];     }     cout << "};";     return 0;
  }
 
 
 
   | 
 
LeetCode 拼写单词
给字符串数组words和字符串chars,问通过chars中的字母,能拼写words中每个单词的最大长度 
输入:words = [“cat”,”bt”,”hat”,”tree”], chars = “atach”
输出:6
解释:
可以形成字符串 “cat” 和 “hat”,所以答案是 3 + 3 = 6。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | int countCharacters(vector<string>& words, string chars) {        char ch[26] = {0};        int res = 0;        for(char c : chars)            ch[c-'a']++;               for(string word : words){            char wd[26] = {0};              for(char c : word)                wd[c-'a']++;              res += word.size();            for(char c:word){                if(wd[c-'a'] > ch[c-'a']){                      res -= word.size();                    break;                }            }        }        return res;       }
  | 
 
LeetCode 二进制求和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
   | string addBinary(string a, string b) {         int lena = a.size();         int lenb = b.size();              while(lena > lenb)         {             b = '0' + b;             ++lenb;         }         while(lena < lenb)         {             a = '0' + a;             ++lena;         }         for(int i = a.size() - 1; i > 0; --i)         {             a[i] = a[i] -'0' + b[i];             if(a[i] >= '2')             {                 a[i] = (a[i]-'0') % 2 +'0';                   a[i-1] = a[i-1] + 1;               }         }
          a[0] = a[0] - '0' + b[0];         if(a[0] >= '2')         {             a[0] = (a[0]-'0') %2 +'0';             a = '1' + a;            }         return a;     }
  | 
 
LeetCode 翻转字符串的每个单词
实战删去字符串开头、结尾的空格,中间不能多于一个空格。
思路还是使用栈,如果把处理空格放在拼接栈的字符串会有问题…于是就直接在放栈之前就处理了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
   | string reverseWords(string s) {        stack<string>st;        string str = "";        string ans = "";        int left = 0, right = s.size() - 1;        int blank = 0;             while(left<=right && s[left] == ' ')            left++;        while(left<=right && s[right] == ' ')            right--;                  for(int i = left; i <= right; i++){            if(s[i] != ' '){                blank = 0;                str += s[i];            }            else{                blank++;                if(blank > 1){                    continue;                }                else{                    st.push(str);                    st.push(" ");                     str = "";                }            }        }        st.push(str);       	        while(!st.empty()){            ans += st.top();            st.pop();        }        return ans;    }
  |