10.12-10.25 字符串

//这周做一下字符串的题目,感觉时间越来越不够用==

1.487-3279(简单题)

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <fstream>
11 #include <cstdlib>
12 #define NDEBUG
13 #include <cassert>
14 
15 using namespace std;
16 
17 #define INF 1000000007
18 #define MIN(a, b) (a > b ? b : a)
19 #define MAX(a, b) (a > b ? a : b)
20 #define MAXN 105
21 
22 int ch[400];
23 char c[50];
24 
25 void init()
26 {
27     ch['A'] = ch['B'] = ch['C'] = 2;
28     ch['D'] = ch['E'] = ch['F'] = 3;
29     ch['G'] = ch['H'] = ch['I'] = 4;
30     ch['J'] = ch['K'] = ch['L'] = 5;
31     ch['M'] = ch['N'] = ch['O'] = 6;
32     ch['P'] = ch['R'] = ch['S'] = 7;
33     ch['T'] = ch['U'] = ch['V'] = 8;
34     ch['W'] = ch['X'] = ch['Y'] = 9;
35 }
36 
37 int main()
38 {
39     int n, flag = 0;
40     init();
41     scanf("%d", &n);
42     getchar();
43     map<int, int> mp;
44     for (int i = 0; i < n; ++i)
45     {
46         gets(c);
47         int tmp = 0;
48         for (int j = 0; j < strlen(c); ++j)
49         {
50             if (c[j] == '-') continue;
51             if (c[j] >= 'A' && c[j] <= 'Z') tmp = tmp*10+ch[c[j]];
52             else if (c[j] >= '0' && c[j] <= '9') tmp = tmp*10 + (c[j]-'0');
53         }
54         mp[tmp]++;
55     }
56     for (map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it)
57         if (it->second > 1 ) flag = 1,printf("%03d-%04d %d
", (it->first)/10000, (it->first)%10000, it->second);
58     if (!flag) puts("No duplicates. ");
59     
60     return 0;
61 }
View Code

 2.Crazy Search(简单题,karp-Rabin)

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <fstream>
11 #include <cstdlib>
12 using namespace std;
13 
14 #define INF 1000000007
15 #define MIN(a, b) (a > b ? b : a)
16 #define MAX(a, b) (a > b ? a : b)
17 #define MAXN 105
18 
19 int n, nc;
20 char ch[1000000];
21 int c[400];
22 int t[16000005];
23 
24 int main()
25 {
26     while (~scanf("%d%d", &n, &nc))
27     {
28         scanf("%s", ch);
29         int len = strlen(ch), cnt = 0;
30         for (int i = 0; i < len; ++i)
31         {
32             if (c[ch[i]] == 0) c[ch[i]] = ++cnt;
33             if (cnt == nc) break;
34         }
35         int num = 0, val = 1;
36         for (int i = 0; i < n; ++i)
37         {
38             num = num*nc+c[ch[i]]-1;
39             val *= nc;    
40         }
41         val /= nc;
42         t[num] = 1;
43         int ans = 1;
44         for (int i = 1; i <= len-n; ++i)
45         {
46             num = (num-(c[ch[i-1]]-1)*val)*nc+c[ch[i+n-1]]-1;
47             if (!t[num]) t[num] = 1, ans++;
48         }
49         printf("%d
", ans);
50     }
51     return 0;    
52 }
View Code

 3.Game of Life(简单偏上一点点)

 1 //模拟人生游戏,比较简单,题目要求原地操作,用特殊值即可
 2 int dir[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
 3 class Solution {
 4 public:
 5     void gameOfLife(vector<vector<int>>& board) {
 6         if (board.size() == 0) return ;
 7         for (int i = 0; i < board.size(); ++i)
 8         {
 9             for (int j = 0; j < board[i].size(); ++j)
10             {
11                 int die = 0, live = 0;
12                 for (int k = 0; k < 8; ++k)
13                 {
14                     int xx = i + dir[k][0];
15                     int yy = j + dir[k][1];
16                     if (xx >= 0 && xx < board.size() && yy >= 0 && yy < board[i].size())
17                     {
18                         if (board[xx][yy] == 2) die++;
19                         else if (board[xx][yy] == 1) live++;
20                         else if (board[xx][yy] == 0) die++;
21                         else live++;
22                     }
23                 }
24                 if (board[i][j])
25                 {
26                     if (live < 2) board[i][j] = -1;
27                     if (live == 2 || live == 3) continue;
28                     if (live > 3) board[i][j] = -1;
29                 }
30                 else if (live == 3) board[i][j] = 2;
31             }
32         }
33         for (int i = 0; i < board.size(); ++i)
34             for (int j = 0; j < board[i].size(); ++j)
35                 board[i][j] = board[i][j] > 0 ? 1 : 0;
36     }
37 };
View Code

4.Nim Game(简单题)

 1 //简单博弈
 2 class Solution {
 3 public:
 4     bool canWinNim(int n) {
 5         if (n < 4) return true;
 6         if ((n-1)%4 == 0) return true;
 7         if ((n-2)%4 == 0) return true;
 8         if ((n-3)%4 == 0) return true;
 9         return false;
10     }
11 };
View Code

 5.Subsets II(中等题)

 1 //奇丑无比的代码……
 2 class Solution {
 3 public:
 4     vector<int> num;
 5     vector<vector<int>> ans;
 6     int len = 0, size, cnt;
 7     void work(int idx, vector<int>& tmp)
 8     {
 9         if (tmp.size() > size) return ;
10         if (idx == len)
11         {
12             if (cnt != size) return ;
13             else ans.push_back(tmp);
14         }
15         if (size == cnt)
16         {
17             ans.push_back(tmp);
18             return ;
19         }
20         for (int i = idx; i < len; ++i)
21         {
22             tmp.push_back(num[i]);
23             cnt++;
24             work(i+1, tmp);
25             tmp.pop_back();
26             cnt--;
27         }
28     }
29     
30     vector<vector<int>> subsetsWithDup(vector<int>& nums) {
31         sort(nums.begin(), nums.end());
32         num = nums;
33         len = nums.size();
34         for (int i = 0; i <= nums.size(); ++i)
35         {
36             vector<int> tmp;
37             size = i;
38             cnt = 0;
39             work(0, tmp);
40         }
41         vector<vector<int>> ret;
42         sort(ans.begin(), ans.end());
43         ret.push_back(ans[0]);
44         for (int i = 1; i < ans.size(); ++i)
45         {
46             if (ans[i] != ans[i-1]) ret.push_back(ans[i]);
47         }
48         return ret;
49     }
50 };
View Code

 6.Convert Sorted List to Binary Search Tree(中等题)

 1 //两个指针,快慢指针找到中点,然后递归
 2 /**
 3  * Definition for singly-linked list.
 4  * struct ListNode {
 5  *     int val;
 6  *     ListNode *next;
 7  *     ListNode(int x) : val(x), next(NULL) {}
 8  * };
 9  */
10 /**
11  * Definition for a binary tree node.
12  * struct TreeNode {
13  *     int val;
14  *     TreeNode *left;
15  *     TreeNode *right;
16  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
17  * };
18  */
19 class Solution {
20 public:
21     TreeNode* sortedListToBST(ListNode* head) {
22         if (head == NULL) return NULL;
23         ListNode *t1, *t2, *pre = NULL;
24         t1 = head, t2 = head;
25         while (t1->next != NULL && t2 != NULL && t2->next != NULL)
26         {
27             pre = t1;
28             t1 = t1->next;
29             t2 = t2->next;
30             if (t2->next == NULL) break;
31             t2 = t2->next;
32         }
33         TreeNode* node = new TreeNode(t1->val);
34         if (pre != NULL)
35         {
36             pre->next = NULL;
37             node->left = sortedListToBST(head);
38         }
39         node->right = sortedListToBST(t1->next);
40         return node;
41     }
42 };
View Code

 7.Peeking Iterator

 1 // Below is the interface for Iterator, which is already defined for you.
 2 // **DO NOT** modify the interface for Iterator.
 3 class Iterator {
 4     struct Data;
 5     Data* data;
 6 public:
 7     Iterator(const vector<int>& nums);
 8     Iterator(const Iterator& iter);
 9     virtual ~Iterator();
10     // Returns the next element in the iteration.
11     int next();
12     // Returns true if the iteration has more elements.
13     bool hasNext() const;
14 };
15 
16 
17 class PeekingIterator : public Iterator {
18 public:
19     PeekingIterator(const vector<int>& nums) : Iterator(nums) {
20         this->next();
21     }
22 
23     // Returns the next element in the iteration without advancing the iterator.
24     int peek() {
25         return next_val;
26     }
27 
28     // hasNext() and next() should behave the same as in the Iterator interface.
29     // Override them if needed.
30     int next() {
31         int ret = next_val;
32         if (Iterator::hasNext())
33         {
34             b_next = true;
35             next_val = Iterator::next();
36         }
37         else b_next = false;
38     
39         return ret;
40     }
41 
42     bool hasNext() const {
43         return  b_next;
44     }
45 private:
46     bool b_next;
47     int next_val;
48 };
View Code

 8.House Robber II(简单DP)

 1 class Solution {
 2 public:
 3     int rob(vector<int>& nums) {
 4         if (nums.size() == 0) return 0;
 5         int ans = 0;
 6         int dp[1000][2];
 7         dp[0][0] = dp[0][1] = 0;
 8         for (int i = 1; i < nums.size(); ++i)
 9         {
10             dp[i][0] = dp[i-1][1];
11             dp[i][1] = dp[i-1][0]+nums[i];
12             dp[i][1] = max(dp[i][0], dp[i][1]);
13         }
14         ans = max(dp[nums.size()-1][0], dp[nums.size()-1][1]);
15         memset(dp, 0, sizeof(dp));
16         dp[0][0] = dp[0][1] = nums[0];
17         for)(int i = 1; i < nums.size(); ++i)
18         {
19             dp[i][0] = dp[i-1][1];
20             dp[i][1] = dp[i-1][0]+nums[i];
21             if (i == 1) dp[i][1] = nums[1];
22             dp[i][1] = max(dp[i][0], dp[i][1]);
23         }
24         ans = max(ans, dp[nums.size()-1][0]);
25         return ans;
26     } 
27 };
View Code

 9.Bitwise AND of Numbers Range(位操作)

 1 class Solution {
 2 public:
 3     int rangeBitwiseAnd(int m, int n) {
 4         if (m == 0) return 0;
 5         if (m == n) return m;
 6         long long int tmp = 1;
 7         while (tmp < m) tmp *= 2;
 8         if (tmp > m && tmp < n) return 0;
 9         if (tmp == n) return 0;
10         if (tmp*2 <= n) return 0;
11         if (tmp == m && tmp*2 > n) return m; 
12         return tmp/2+rangeBitwiseAnd(m-tmp/2, n-tmp/2);
13     }
14 };
View Code

 10.Reverse Linked List II(中等)

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* reverse(ListNode* head)
12     {
13         if (head == NULL) return NULL;
14         if (head->next == NULL) return head;
15         ListNode *fir = head, *sec = head->next;
16         head->next = NULL;
17         while (sec != NULL)
18         {
19             ListNode* tmp = sec->next;
20             sec->next = fir;
21             fir = sec;
22             sec = tmp;
23         }
24         return fir;
25     }
26     ListNode* reverseBetween(ListNode* head, int m, int n) {
27         int idx = 1;
28         ListNode *first = new ListNode(0);
29         first->next = head;
30         ListNode *pre = first, *tmp = head;
31         while (idx != m)
32         {
33             pre = tmp;
34             tmp = tmp->next;
35             idx++;
36         }
37         ListNode *l1 = tmp;
38         while (idx != n)
39         {
40             tmp = tmp->next;
41             idx++;
42         }
43         ListNode *last = tmp->next;
44         tmp->next = NULL;
45         ListNode* ret = reverse(l1);
46         pre->next = ret;
47         while (ret->next != NULL) ret = ret->next;
48         ret->next = last;
49         ret = first->next;
50         delete first;
51         return ret;
52     }
53 };
View Code

 11.Next Permutation(中等偏下)

 1 class Solution {
 2 public:
 3     void nextPermutation(vector<int>& nums) {
 4         int flag = 1;
 5         for (int i = nums.size()-1; i > 0; --i)
 6         {
 7             if (nums[i] > nums[i-1])
 8             {
 9                 for (int j = nums.size()-1; j >= i; j--)
10                 {
11                     if (nums[j] > nums[i-1]) 
12                     {
13                         swap(nums[j], nums[i-1]);
14                         flag = 0;
15                         break;
16                     }
17                 }
18                 int j = i, k = nums.size()-1;
19                 while (j < k)
20                 {
21                     swap(nums[j], nums[k]);
22                     j++, k--;
23                 }
24                 break;
25             }
26         }
27         if (flag)
28         {
29             int j = 0, k = nums.size()-1;
30             while (j < k)
31             {
32                 swap(nums[j], nums[k]);
33                 j++, k--;
34             }
35         }
36     }
37 };
View Code

 12.Minimum Size Subarray Sum(two points)

 1 class Solution {
 2 public:
 3     int minSubArrayLen(int s, vector<int>& nums) {
 4         vector<int> dp(nums.size()+1, 0);
 5         if (nums.size() == 0) return 0;
 6         dp[1] = nums[0];
 7         for (int i = 1; i < nums.size(); ++i)
 8             dp[i+1] = dp[i]+nums[i];
 9         int ans = 1e9;
10         int i = 0, j = 1;
11         while (i <= j && j <= nums.size())
12         {
13             if (dp[j]-dp[i] >= s)
14             {
15                 if (j-i < ans) ans = j-i;
16                 i++;
17             }
18             else j++;
19         }
20         if (ans == 1e9) return 0;
21         return ans;
22     }
23 };
View Code

 13.Palindrome(曼彻斯特算法)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <string>
 6 #include <cstring>
 7 #include <vector>
 8 #include <list>
 9 #include <queue>
10 
11 using namespace std;
12 
13 char ch1[1000010];
14 char ch2[2000010];
15 int p[2000010];
16 
17 void build()
18 {
19     ch2[0] = '@';
20     ch2[1] = '#';
21     int i, len = strlen(ch1);
22     for (i = 0; i < len; ++i)
23     {
24         ch2[2*i+2] = ch1[i];
25         ch2[2*i+3] = '#';
26     }
27     ch2[2*i+2] = '';
28 }
29 
30 int solve()
31 {
32     p[0] = p[1] = 1;
33     int id = 1, mx = 2, maxlen = 0;
34     for (int i = 2; ch2[i]; ++i)
35     {
36         p[i] = mx > i ? min(p[2*id-i], mx-i) : 1;
37         while (ch2[i+p[i]] == ch2[i-p[i]]) p[i]++;
38         if (i+p[i] > mx)
39         {
40             mx = i+p[i];
41             id = i;
42         }
43         maxlen = max(maxlen, p[i]-1);
44     }
45     return maxlen;
46 }
47 
48 int main()
49 {
50     int ncas = 1;
51     while (~scanf("%s", ch1) && strcmp(ch1, "END") != 0)
52     {
53         build();
54         printf("Case %d: %d
", ncas++, solve());
55     }
56 
57     return 0;
58 }
View Code

 14.Find the Duplicate Number(回溯/回文串/中等题目)

 1 int dp[1000][1000];
 2 class Solution {
 3 public:
 4     int judge(int left, int right, string& str)
 5     {
 6         while (left < right)
 7         {
 8             if (str[left] == str[right])
 9                 left++, right--;
10             else return 0;
11         }
12         return 1;
13     }
14     void work(string& s, int idx, vector<string>&add, vector<vector<string>>&ans)
15     {
16         if (idx == s.length())
17         {
18             ans.push_back(add);
19             return ;
20         }
21         string str = "";
22         for (int i = idx; i < s.length(); ++i)
23         {
24             str += s[i];
25             if (dp[idx][i])
26             {
27                 add.push_back(str);
28                 work(s, i+1, add, ans);
29                 add.pop_back();
30             }
31         }
32     }
33     
34     vector<vector<string>> partition(string s) {
35         memset(dp, 0, sizeof(dp));
36         for (int i = 0; i < s.length(); ++i)
37             for (int j = i; j < s.length(); ++j)
38                 dp[i][j] = judge(i, j, s);
39         vector<string> add;
40         vector<vector<string>> ans;
41         add.clear();
42         ans.clear();
43         work(s, 0, add, ans);
44         return ans;
45     }
46 };
View Code

 15.Find the Duplicate Number(Cycle detection/难题(难在问题转化)) 

 1 class Solution {
 2 public:
 3     int findDuplicate(vector<int>& nums) {
 4         int slow = 0;
 5         int fast = 0;
 6         do{
 7             slow = nums[slow];
 8             fast = nums[nums[fast]];
 9         } while(slow != fast);
10         int find = 0;
11         while(find != slow){
12             slow = nums[slow];
13             find = nums[find];
14         }
15         return find;
16     }
17 };
View Code

 16.Implement Trie (Prefix Tree)(字典树/中等题)

 1 class TrieNode {
 2 public:
 3     bool flag;
 4     TrieNode* node[26];
 5     // Initialize your data structure here.
 6     TrieNode() {
 7         flag = false;
 8         memset(node, 0, sizeof(node));
 9     }
10 };
11 
12 class Trie {
13 public:
14     Trie() {
15         root = new TrieNode();
16     }
17 
18     // Inserts a word into the trie.
19     void insert(string word) {
20         TrieNode *top = root;
21         for (int i = 0; i < word.length(); ++i)
22         {
23             if (top->node[word[i]-'a'] == NULL)
24             {
25                 TrieNode* tmp = new TrieNode();
26                 top->node[word[i]-'a'] = tmp;
27             }
28             top = top->node[word[i]-'a'];
29         }
30         top->flag = true;
31     }
32 
33     // Returns if the word is in the trie.
34     bool search(string word) {
35         TrieNode* top = root;
36         for (int i = 0; i < word.length(); ++i)
37         {
38             if (top->node[word[i]-'a'] == NULL) return false;
39             top = top->node[word[i]-'a'];
40         }
41         return top->flag;
42     }
43 
44     // Returns if there is any word in the trie
45     // that starts with the given prefix.
46     bool startsWith(string prefix) {
47         TrieNode *top = root;
48         for (int i = 0; i < prefix.length(); ++i)
49         {
50             if (top->node[prefix[i]-'a'] == NULL) return false;
51             top = top->node[prefix[i]-'a'];
52         }
53         return true;
54     }
55 
56 private:
57     TrieNode* root;
58 };
View Code

 17.Copy List with Random Pointer(STL/map/中等偏上)

 1 /**
 2  * Definition for singly-linked list with a random pointer.
 3  * struct RandomListNode {
 4  *     int label;
 5  *     RandomListNode *next, *random;
 6  *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     RandomListNode *copyRandomList(RandomListNode *head) {
12         RandomListNode *t = head, *first = NULL, *pre;
13         map<RandomListNode*, RandomListNode*> mp;
14         while (t)
15         {
16             RandomListNode *tmp = new RandomListNode(t->label);
17             if (first == NULL) first = tmp, pre = tmp;
18             else pre->next = tmp, pre = tmp;
19             mp[t] = tmp;
20             t = t->next;
21         }
22         t = head;
23         pre = first;
24         while (t)
25         {
26             if (t->random != NULL)
27                 pre->random = mp[t->random];
28             t = t->next;
29             pre = pre->next;
30         }
31         return first;
32     }
33 };
View Code

 18.Oulipo(KMP)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <string>
 6 #include <cstring>
 7 #include <vector>
 8 #include <list>
 9 #include <queue>
10 
11 using namespace std;
12 
13 int nxt[10010];
14 char ch1[1000010], ch2[10010];
15 
16 void getNext()
17 {
18     nxt[0] = -1;
19     int j = -1, len = strlen(ch2);
20     for (int i = 1; i < len; ++i)
21     {
22         while (j >= 0 && ch2[i] != ch2[j+1]) j = nxt[j];
23         if (ch2[j+1] == ch2[i]) j++;
24         nxt[i] = j;
25     }
26 }
27 
28 int work()
29 {
30     int cnt = 0;
31     int j = -1, len1 = strlen(ch1), len2 = strlen(ch2);
32     for (int i = 0; i < len1; ++i)
33     {
34         while (j >= 0 && ch2[j+1] != ch1[i]) j = nxt[j];
35         if (ch2[j+1] == ch1[i]) j++;
36         if (j+1 == len2)
37         {
38             cnt++;
39             j = nxt[j];
40         }
41     }
42     return cnt;
43 }
44 
45 int main()
46 {
47     int t;
48     scanf("%d", &t);
49     while (t--)
50     {
51         scanf("%s", ch2);
52         scanf("%s", ch1);
53         getNext();
54         printf("%d
", work());
55     }
56 
57     return 0;
58 }
View Code
原文地址:https://www.cnblogs.com/JustForCS/p/4873689.html