One-by-one to solve the Sequence Problem (1)
来源:互联网 发布:赵丽颖野心知乎 编辑:程序博客网 时间:2024/04/29 02:38
One-by-one to solve the Sequence Problem (1)
1400 : Composition (from hihocoder)
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Alice writes an English composition with a length of N characters. However, her teacher requires that M illegal pairs of characters cannot be adjacent, and if ‘ab’ cannot be adjacent, ‘ba’ cannot be adjacent either.
In order to meet the requirements, Alice needs to delete some characters.
Please work out the minimum number of characters that need to be deleted.
输入
The first line contains the length of the composition N.
The second line contains N characters, which make up the composition. Each character belongs to ‘a’..’z’.
The third line contains the number of illegal pairs M.
Each of the next M lines contains two characters ch1 and ch2,which cannot be adjacent.
For 20% of the data: 1 ≤ N ≤ 10
For 50% of the data: 1 ≤ N ≤ 1000
For 100% of the data: 1 ≤ N ≤ 100000, M ≤ 200.
输出
One line with an integer indicating the minimum number of characters that need to be deleted.
样例提示
Delete ‘a’ and ‘d’.
样例输入
5abcde3acabde
样例输出
2
Code
#include <iostream>#include <list>#include <vector>#include <string>#include <unordered_map>#include <unordered_set>#include <utility>using namespace std;typedef unsigned int unint;typedef unordered_set<string> setstr;typedef unordered_map<char, unint> mapchar;typedef pair<char, unint> pcu;int main() { int N = 0; while (cin >> N) { string ts; cin >> ts; list<char> mstr(ts.begin(), ts.end()); int m = 0; cin >> m; setstr mp; for (int i = 0; i < m; ++i) { string s1; cin >> s1; mp.insert(s1); char tc = s1[0]; s1[0] = s1[1]; s1[1] = tc; mp.insert(s1); } mapchar mmp; mmp.insert(pair<char,unint>(ts[0], 0)); for (int i =1;i<N; ++i) { char c = ts[i]; mapchar tmmp; string s11(2,'0'); auto resf0 = mmp.find(c); tmmp.insert(pcu(c, i)); s11[1] = c; for (auto it : mmp) { // delete it.second += 1; auto resf = tmmp.find(it.first); if (resf == tmmp.end()) { tmmp.insert(it); } else { if (resf->second > it.second) resf->second = it.second; } // keep it it.second -= 1; s11[0] = it.first; auto resf1 = mp.find(s11); if (resf1 == mp.end()) { // insert it auto resf11 = tmmp.find(c); if (resf11 == tmmp.end()) { tmmp.insert(pcu(c, it.second)); } else { if (resf11->second > it.second) resf11->second = it.second; } } }// for it : mmp mmp = tmmp; } unint res1 = 0x0fffffff; for (auto i : mmp) { if (res1 > i.second) res1 = i.second; } cout << res1 << endl; }// end of while return 0;}
- One-by-one to solve the Sequence Problem (1)
- One-by-one to solve the Sequence Problem (2)
- How to solve "one or more projects in the solution were not loaded correctly" problem?
- JPA -the annotation about one to one
- veste barbour homme the mother Chen Fengju two cronies to pick up one by one
- Sequence one
- one by one note
- Solve Questions chapter one
- Problem 14 - Find the longest sequence using a starting number under one million.
- one problem
- Sort the array one by one without changing current array
- Leetcode #9 Discussion: Only one integer to solve it.
- one to one
- Hibernate one-to-one
- hibernate :one-to-one
- 一对一 one-to-one
- hibernate:one-to-one
- one-to-one 映射
- leetcode134. Gas Station
- 好玩的editText
- 设计模式——工厂模式
- Unity中的Path对应各平台中的Path
- 数据分析随记
- One-by-one to solve the Sequence Problem (1)
- WDTabbarController
- 二叉排序树的操作
- poj 1050
- Python基本语法笔记
- 浅析Java中的final关键字
- BoxLayout边框布局
- 跳转动画
- Jvm中的直接引用和符号引用