Initial commit
This commit is contained in:
61
2019/republic/12_d1/anagrame.cpp
Normal file
61
2019/republic/12_d1/anagrame.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ifstream input_stream;
|
||||
ofstream output_stream;
|
||||
|
||||
|
||||
int findAnagramPos(string content, string title) {
|
||||
const uint32_t CONTENT_LEN = content.length();
|
||||
const uint32_t TITLE_LEN = title.length();
|
||||
|
||||
unordered_map<char, uint32_t> target, anagram;
|
||||
for (uint32_t i = 0; i < TITLE_LEN; i++) {
|
||||
target[title[i]]++;
|
||||
anagram[content[i]]++;
|
||||
}
|
||||
|
||||
if (target == anagram) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (uint32_t i = TITLE_LEN; i < CONTENT_LEN; i++) {
|
||||
/**
|
||||
Old first character pos = i - TITLE_LEN
|
||||
New first character pos = i - TITLE_LEN + 1
|
||||
Old last character pos = i - 1
|
||||
New last character pos = i
|
||||
*/
|
||||
char firstChar = content[i - TITLE_LEN];
|
||||
anagram[firstChar]--;
|
||||
|
||||
if (anagram[firstChar] == 0) {
|
||||
anagram.erase(firstChar);
|
||||
}
|
||||
|
||||
anagram[content[i]]++;
|
||||
|
||||
if (target == anagram) {
|
||||
return i - TITLE_LEN;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
input_stream.open("input.txt");
|
||||
|
||||
string content, title;
|
||||
input_stream >> content;
|
||||
input_stream >> title;
|
||||
|
||||
cout << findAnagramPos(content, title) << endl;
|
||||
|
||||
input_stream.close();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user