bupt summer training for 16 #8 ——字符串處理

https://vjudge.net/contest/175596#overview

?

A.設第i次出現的位置左右端點分別為Li,Ri

初始化L0 = 0,則有ans = sum{ (L[i] - L[i-1]) * (n + 1 - Ri) }

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 int last = 0;
 9 
10 char s[5010];
11 
12 long long ans;
13 
14 int main() {
15     scanf("%s", s + 1);
16     int n = strlen(s + 1);
17     for(int i = 1;i <= n;i ++) {
18         if(i + 3 <= n && s[i] == 'b' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'r') {
19             ans += 1ll * (i - last) * (n + 1 - i - 3);
20             last = i;
21             i += 3;
22         }
23     }
24     cout << ans;
25     return 0;
26 }
View Code

?

B.AC自動機板子題,我的板子常數很大

 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 const int maxn = 500010;
 8 
 9 struct trie {
10     int next[maxn][26], fail[maxn], end[maxn];
11     int L, root;
12     queue <int> q;
13 
14     int newnode() {
15         for(int i = 0;i < 26;i ++)
16             next[L][i] = -1;
17         end[L] = 0;
18         return L ++;
19     }
20 
21     void clear() {
22         L = 0;
23         root = newnode();
24     }
25 
26     int idx(char c) {
27         return c - 'a';
28     }
29 
30     void insert(char *buf) {
31         int len = strlen(buf), now = root, c;
32         for(int i = 0;i < len;i ++) {
33             c = idx(buf[i]);
34             if(next[now][c] == -1)
35                 next[now][c] = newnode();
36             now = next[now][c];
37         }
38         end[now] ++;
39     }
40 
41     void build() {
42         for(int i = 0;i < 26;i ++) {
43             if(next[root][i] == -1)
44                 next[root][i] = root;
45             else {
46                 fail[next[root][i]] = root;
47                 q.push(next[root][i]);
48             }
49         }
50         while(!q.empty()) {
51             int now = q.front();
52             q.pop();
53             for(int i = 0;i < 26;i ++) {
54                 if(next[now][i] == -1)
55                     next[now][i] = next[fail[now]][i];
56                 else {
57                     fail[next[now][i]] =  next[fail[now]][i];
58                     q.push(next[now][i]);
59                 }
60             }
61         }
62     }
63 
64     int query(char *buf) {
65         int len = strlen(buf), now = root, res = 0, tmp;
66         for(int i = 0;i < len;i ++) {
67             tmp = now = next[now][idx(buf[i])];
68             while(tmp != root) {
69                 res += end[tmp];
70                 end[tmp] = 0;
71                 tmp = fail[tmp];
72             }
73         }
74         return res;
75     }
76 };
77 
78 trie ac;
79 
80 int Case, n;
81 
82 char buf[1000010];
83 
84 int main() {
85     scanf("%d", &Case);
86     while(Case --) {
87         scanf("%d", &n), ac.clear();
88         while(n --) scanf("%s", buf), ac.insert(buf);
89         scanf("%s", buf), ac.build();
90         printf("%d\n", ac.query(buf));
91     }
92     return 0;
93 }
View Code

?

C.考察對KMP中next數組的理解,由一個串重復而來

所以就是max(i - next[i])

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 char s[1000010];
 8 
 9 int nex[1000010];
10 
11 int main() {
12     int i, j, ans, len;
13     while(~scanf("%s", s)) {
14         len = strlen(s);
15         nex[0] = -1;
16         ans = 0;
17         for(i = 1;i < len;i ++) {
18             j = nex[i - 1];
19             while(j >= 0 && s[j + 1] != s[i]) j = nex[j];
20             if(s[j + 1] == s[i]) {
21                 nex[i] = j + 1;
22                 ans = max(ans, i - nex[i]);
23             }
24             else nex[i] = -1, ans = max(ans, i + 1);
25         }
26         printf("%d\n", ans);
27     }
28     return 0;
29 }
View Code

?

D.令a[i] -= a[i + 1],題目就變成了

求數列中出現次數不小于2次的最長重復子串

后綴數組一個典型問題,二分子串長度即可

(考場上觀察了半天手里板子的接口...然后放棄了)

?

E.先假設要由空串刷成串2,區間DP即可

dp[i][j]代表把 i-j 這段刷成串2需要的最少次數

可能分成幾段分開去刷,所以不能直接ans = dp[L][R] (s1[L] != s2[L],s1[R] != s2[R])

利用f[i]代表 1-i 這段由串1刷成串2的最少次數

ans = f[n]

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 int n, dp[110][110], f[110];
 8 
 9 char s1[110], s2[110];
10 
11 int main() {
12     while(~scanf("%s %s", s1 + 1, s2 + 1)) {
13         n = strlen(s1 + 1);
14         memset(dp, 0x3f, sizeof dp);
15         for(int d = 1;d <= n;d ++)
16             for(int i = 1;i + d - 1 <= n;i ++) {
17                 int j = i + d - 1;
18                 if(i == j) dp[i][i] = 1;
19                 else if(j == i + 1) dp[i][j] = 2 - (s2[i] == s2[j]);
20                 else {
21                     for(int k = i;k < j;k ++)
22                         dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] - (s2[i] == s2[k + 1]));
23                 }
24             }
25         for(int i = 1;i <= n;i ++) {
26             f[i] = dp[1][i];
27             if(s1[i] == s2[i]) f[i] = min(f[i - 1], f[i]);
28             else 
29                 for(int j = 1;j < i;j ++)
30                     f[i] = min(f[i],  f[j] + dp[j + 1][i]);
31         }
32         printf("%d\n", f[n]);
33     }
34     return 0;
35 }
View Code

?

F.簡單的字典樹

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 const int maxn = 6000010;
 5 
 6 struct trie {
 7     int ch[maxn][2];
 8     int val[maxn];
 9     int siz;
10 
11     void init() {
12         siz = 1;
13         memset(ch, 0, sizeof ch);
14         memset(val, 0, sizeof val);
15     }
16 
17     void insert(int x) {
18         int i, u, c;
19         int num[40] = {0};
20         for(i = 0;i < 30;i ++)
21             num[i] = x & (1 << i);
22         for(i = u = 0;i < 30;i ++) {
23             c = (num[29 - i] != 0);
24             if(!ch[u][c]) ch[u][c] = siz ++;
25             u = ch[u][c], val[u] ++;
26         }
27         val[0] ++;
28     }
29 
30     void de1ete(int x) {
31         int i, u, c;
32         int num[40] = {0};
33         for(i = 0;i < 30;i ++)
34             num[i] = x & (1 << i);
35         for(i = u = 0;i < 30;i ++) {
36             u = ch[u][(num[29 - i] != 0)];
37             val[u] --;
38         }
39         val[0] --;
40     }
41 
42     void query(int x) {
43         int i, u, c, ans = 0;
44         int num[40] = {0};
45         for(i = 0;i < 30;i ++)
46             num[i] = x & (1 << i);
47         for(i = u = 0;i < 30;i ++) {
48             c = !(num[29 - i] != 0);
49             if(ch[u][c] && val[ch[u][c]]) ans |= (1 << (29 - i)), u = ch[u][c];
50             else u = ch[u][!c];
51         }
52         printf("%d\n", ans);
53     }
54 };
55 
56 trie now;
57 
58 int n, x;
59 
60 char str[5];
61 
62 int main() {
63     now.init();
64     now.insert(0);
65     scanf("%d", &n);
66     while(n --) {
67         scanf("%s %d", str, &x);
68         switch(str[0]) {
69             case '+':now.insert(x);break;
70             case '-':now.de1ete(x);break;
71             case '?':now.query(x);break;
72         }
73     }
74     return 0;
75 }
View Code

?

G.

?

H.

?

I.

?

J.

?

K.最長回文子串,直接上馬拉車

板子不長,mp[i] - 1 表示以 i 為中心的最長回文串長度

 1 #include <map>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 const int maxn = 1000010;
 9 
10 char str[3], s[maxn], ma[maxn], ans[maxn];
11 
12 int mp[maxn], l, len;
13 
14 map <char, char> p;
15 
16 void manacher() {
17     l = 0;
18     ma[l ++] = '$';
19     ma[l ++] = '#';
20     for(int i = 0;i < len;i ++)
21         ma[l ++] = s[i], ma[l ++] = '#';
22     ma[l] = 0;
23     int mx = 0, id = 0;
24     for(int i = 0;i < l;i ++) {
25         mp[i] = mx > i ? min(mp[2 * id - i], mx - i) : 1;
26         while(ma[i + mp[i]] == ma[i - mp[i]]) mp[i] ++;
27         if(i + mp[i] > mx) mx = i + mp[i], id = i;
28     }
29 }
30 
31 int main() {
32     while(~scanf("%s %s", str, s)) {
33         len = strlen(s);
34         manacher();
35         int leng = 0, pos = -1;
36         for(int i = 0;i < l;i ++)
37             if(mp[i] > leng)
38                 leng = mp[i], pos = i;
39         leng --;
40         if(leng == 1) {
41             puts("No solution!");
42             continue;
43         }
44         if(pos & 1) {
45             printf("%d %d\n", pos / 2 - leng / 2, pos / 2 - leng / 2 + leng - 1);
46             for(int i = pos / 2 - leng / 2, j = 1;j <= leng;i ++, j ++)
47                 ans[j] = s[i];
48         }
49         else {
50             printf("%d %d\n", pos / 2 - leng / 2 - 1, pos / 2 - leng / 2 + leng - 2);
51             for(int i = pos / 2 - leng / 2 - 1, j = 1;j <= leng;i ++, j ++)
52                 ans[j] = s[i];
53         }
54         ans[leng + 1] = 0;
55         int dis = 'a' - str[0];
56         for(int i = 0;i < 26;i ++)
57             p['a' + i] = 'a' + (i + dis + 26) % 26;
58         for(int i = 1;i <= leng;i ++)
59             ans[i] = p[ans[i]];
60         puts(ans + 1);
61     }
62     return 0;
63 }
View Code

?

L.變換同D題,然后就是裸的KMP了

 1 #include <map>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 const int maxn = 1000010;
 9 
10 char str[3], s[maxn], ma[maxn], ans[maxn];
11 
12 int mp[maxn], l, len;
13 
14 map <char, char> p;
15 
16 void manacher() {
17     l = 0;
18     ma[l ++] = '$';
19     ma[l ++] = '#';
20     for(int i = 0;i < len;i ++)
21         ma[l ++] = s[i], ma[l ++] = '#';
22     ma[l] = 0;
23     int mx = 0, id = 0;
24     for(int i = 0;i < l;i ++) {
25         mp[i] = mx > i ? min(mp[2 * id - i], mx - i) : 1;
26         while(ma[i + mp[i]] == ma[i - mp[i]]) mp[i] ++;
27         if(i + mp[i] > mx) mx = i + mp[i], id = i;
28     }
29 }
30 
31 int main() {
32     while(~scanf("%s %s", str, s)) {
33         len = strlen(s);
34         manacher();
35         int leng = 0, pos = -1;
36         for(int i = 0;i < l;i ++)
37             if(mp[i] > leng)
38                 leng = mp[i], pos = i;
39         leng --;
40         if(leng == 1) {
41             puts("No solution!");
42             continue;
43         }
44         if(pos & 1) {
45             printf("%d %d\n", pos / 2 - leng / 2, pos / 2 - leng / 2 + leng - 1);
46             for(int i = pos / 2 - leng / 2, j = 1;j <= leng;i ++, j ++)
47                 ans[j] = s[i];
48         }
49         else {
50             printf("%d %d\n", pos / 2 - leng / 2 - 1, pos / 2 - leng / 2 + leng - 2);
51             for(int i = pos / 2 - leng / 2 - 1, j = 1;j <= leng;i ++, j ++)
52                 ans[j] = s[i];
53         }
54         ans[leng + 1] = 0;
55         int dis = 'a' - str[0];
56         for(int i = 0;i < 26;i ++)
57             p['a' + i] = 'a' + (i + dis + 26) % 26;
58         for(int i = 1;i <= leng;i ++)
59             ans[i] = p[ans[i]];
60         puts(ans + 1);
61     }
62     return 0;
63 }
View Code

轉載于:https://www.cnblogs.com/ytytzzz/p/7275492.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/280936.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/280936.shtml
英文地址,請注明出處:http://en.pswp.cn/news/280936.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

程序員必須知道的HTML常用代碼有哪些?

HTML即超文本標記語言&#xff0c;是目前應用最為廣泛的語言之一&#xff0c;是組成一個網頁的主要語言。在現今這個HTML5華麗麗地占領了整個互聯網的時候&#xff0c;如果想要通過網頁抓住瀏覽者的眼球光靠因循守舊是不行的&#xff0c;程序猿們需要掌握一些必須知道的HTML常用…

公用ip地址查詢_是什么使您無法更改公用IP地址并在Internet上造成嚴重破壞?

公用ip地址查詢What exactly is preventing you (or anyone else) from changing their IP address and causing all sorts of headaches for ISPs and other Internet users? 到底是什么在阻止您(或其他任何人)更改其IP地址并導致ISP和其他Internet用戶感到頭疼&#xff1f; …

Vim的新一代補全插件:coc.nvim

coc.nvim可以同時在nvim和vim8.1上使用。 安裝 參考官方&#xff1a;Install coc.nvim 推薦使用vim-plug插件管理器&#xff0c;在vimrc中添加&#xff1a; Plug neoclide/coc.nvim, {do: { -> coc#util#install()}} 然后輸入命令:PlugInstall 等待插件下載&#xff0c;再等…

MySQL-02:“數據庫”操作基本命令及權限筆記

目錄 數據庫操作 1、顯示數據庫 2、創建數據庫 3、使用數據庫 4、用戶管理 5、授權管理 數據庫操作 1、顯示數據庫 SHOW DATABASES; 默認數據庫&#xff1a;   mysql - 用戶權限相關數據   test - 用于用戶測試數據   information_schema - MySQL本身架構相關數據…

C++STL——概述

一、相關介紹 STL 標準模板庫在編寫代碼的過程中有一些程序經常會被用到&#xff0c;而且需求特別穩定&#xff0c;所以C中把這些常用的模板做了統一的規范&#xff0c;慢慢的就形成了STL提供三種類型的組件: 容器、迭代器和算法&#xff0c;它們都支持泛型程序設計標準容器 順…

固態硬盤可靠性_您可以通過使用較少的總容量來提高硬盤的可靠性嗎?

固態硬盤可靠性Your computer has a massive hard drive that you significantly underuse. Would decreasing the size of the primary partition actually increase the lifespan of the drive? 您的計算機具有大量未充分使用的巨大硬盤驅動器。 減小主分區的大小是否會真正…

接收上傳的multi-file的文件(四)

構建工程 為例創建一個springmvc工程你需要spring-boot-starter-thymeleaf和 spring-boot-starter-web的起步依賴。為例能夠上傳文件在服務器&#xff0c;你需要在web.xml中加入標簽做相關的配置&#xff0c;但在sringboot 工程中&#xff0c;它已經為你自動做了&#xff0c;所…

數據庫讀寫分離 - MyBatis

2019獨角獸企業重金招聘Python工程師標準>>> 由于項目中數據量較大&#xff0c;訪問量也較高&#xff0c;故在數據庫的設計上&#xff0c;采用讀寫分離思想&#xff0c;達到性能要求&#xff01; 簡單科普一下實現讀寫分離的思路 配置及加載數據庫信息&#xff0c;即…

MySQL-03:數據表操作基本命令筆記

目錄 數據表 1、創建表 2、刪除表 3、清空表 4、修改表 5、基本數據類型 數據表 1、創建表 create table 表名(列名 類型 是否可以為空&#xff0c;列名 類型 是否可以為空 )ENGINEInnoDB DEFAULT CHARSETutf8 是否可空&#xff0c;null表示空&#xff0c;非字符串n…

java 怎么調試到第三方庫的內部,在有源碼的情況下

第一步&#xff0c; 把第三方庫加到workspace : https://stackoverflow.com/questions/370814/how-to-set-a-breakpoint-in-eclipse-in-a-third-party-library The most sure-fire way to do this (and end up with something thats actually useful) is to download the sou…

t-mobile頻段_T-Mobile再次被黑客入侵:超過200萬個帳號和地址可能泄漏

t-mobile頻段Attackers may have compromised three percent of T-Mobile’s 77 million customers on Monday, revealing personal information like addresses, phone numbers, and account numbers. 周一&#xff0c;攻擊者可能泄露了T-Mobile 7700萬客戶中的3&#xff05;&…

第二篇 第三章防火防煙分區檢查(一)

倉庫面積可以增加3倍 就是乘以4 要一定條件 : 第二篇 第三章防火防煙分區檢查&#xff08;一&#xff09; 21分鐘處 該題比較有代表性 停車庫 耐火等級允許最大面積 民用建筑防火分區 防煙分區的劃分    防火卷簾控制器的測試 防火閥 裝在通風,空調系統中 只有連在風機主管…

高斯數學

偉大的數學家高斯在9歲那年&#xff0c;用很短的時間完成了從1到100的累加。那原本是老師給學生們出的難題&#xff0c;希望他們能老老實實地待在教室里。高斯的方法很簡單&#xff0c;他發現這是50個101的求和&#xff1a;100&#xff0b;1、99&#xff0b;2、98&#xff0b;3…

Ant Design Blazor 發布 0.13.0,正式支持.NET 7!

時隔3個月&#xff0c;Ant Design Blazor 發布新功能版本 0.13.0&#xff0c;并正式支持.NET 7!大家快去訪問 antblazor.com 體驗吧&#xff01;&#x1f525; 新增 .NET 7 目標框架支持。#2810 ElderJames&#x1f525; 重構 Mentions 組件&#xff0c;修復定位和隱藏問題。#2…

gitlab 分支操作筆記\新建遠程分支\抓取遠程分支\復制遠程\刪除分支

密碼重新輸入與保存 git config --global http.emptyAuth truegit config --global credential.helper store 1.不復制遠程&#xff0c;直接新建遠程分支。&#xff08;非正規操作&#xff09; git init //初始化 git remote add origin http://****/*****/taskboard.git…

如何在Xbox One或PlayStation 4上為Skyrim特別版安裝Mods

The Elder Scrolls V: Skyrim Special Edition is now available on PlayStation 4 and Xbox One, and for the first time, “mods” are available to console gamers. Elder Scrolls V&#xff1a;Skyrim特別版現已在PlayStation 4和Xbox One上可用&#xff0c;并且首次向主…

微軟宣布:PowerBI 已經與 Office 整合,一切更簡單,變革又來了

很多人認為 Office 是 Office&#xff0c;PowerBI 是 PowerBI&#xff0c;怎么在 PPT 中顯示 PowerBI 呢&#xff1f;這種問題以后將再不會存在。微軟已經宣布&#xff0c;PowerBI 已經與 Office 深度整合&#xff0c;在未來的企業中&#xff0c;PowerBI 將與 Word&#xff0c;…

066:ORM查詢條件詳解-startswith和endswith:

ORM查詢條件詳解-startswith和endswith&#xff1a; startswith&#xff1a;判斷某個字段的值是否是以某個值開始的。大小寫敏感。示例代碼如下&#xff1a; articles1 Article.objects.filter(title__startswith"fuck") 以上代碼的意思是提取所有標題以 fuck 字符串…

前端工程師面試題匯總

HTML Doctype作用&#xff1f;嚴格模式與混雜模式如何區分&#xff1f;它們有何意義? HTML5 為什么只需要寫 <!DOCTYPE HTML>&#xff1f; 行內元素有哪些&#xff1f;塊級元素有哪些&#xff1f; 空(void)元素有那些&#xff1f; 頁面導入樣式時&#xff0c;使用lin…

MySQL-04:數據內容操作-增刪改查-基本命令筆記

1、增 insert into 表 (列名,列名...) values (值,值,值...) insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...) insert into 表 (列名,列名...) select (列名,列名...) from 表 2、刪 delete from 表 delete from 表 where id&#xff1d;1 and name&…