Skip to content

Instantly share code, notes, and snippets.

@so77id
Created June 30, 2020 22:40
Show Gist options
  • Save so77id/0a2d33d9c3404efb724c7463092e6427 to your computer and use it in GitHub Desktop.
Save so77id/0a2d33d9c3404efb724c7463092e6427 to your computer and use it in GitHub Desktop.
Sol2
import java.io.*;
import java.util.*;
public class Solution {
static class KMP_String_Matching {
public Vector<Integer> KMPSearch(String pat, String txt)
{
Vector<Integer> find_idxs = new Vector<Integer>();
int M = pat.length();
int N = txt.length();
// create lps[] that will hold the longest
// prefix suffix values for pattern
int lps[] = new int[M];
int j = 0; // index for pat[]
// Preprocess the pattern (calculate lps[]
// array)
computeLPSArray(pat, M, lps);
int i = 0; // index for txt[]
while (i < N) {
if (pat.charAt(j) == txt.charAt(i)) {
j++;
i++;
}
if (j == M) {
find_idxs.add(i-j);
// System.out.println("Found pattern "
// + "at index " + (i - j));
j = lps[j - 1];
}
// mismatch after j matches
else if (i < N && pat.charAt(j) != txt.charAt(i)) {
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return find_idxs;
}
public void computeLPSArray(String pat, int M, int lps[])
{
// length of the previous longest prefix suffix
int len = 0;
int i = 1;
lps[0] = 0; // lps[0] is always 0
// the loop calculates lps[i] for i = 1 to M-1
while (i < M) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else // (pat[i] != pat[len])
{
// This is tricky. Consider the example.
// AAACAAAA and i = 7. The idea is similar
// to search step.
if (len != 0) {
len = lps[len - 1];
// Also, note that we do not increment
// i here
}
else // if (len == 0)
{
lps[i] = len;
i++;
}
}
}
}
}
static Vector<Integer> process_idxs(Vector<Integer> idxs, int size) {
Vector<Integer> res = new Vector<Integer>();
for(int i=0;i<idxs.size();){
res.add(idxs.get(i));
int step = 0;
while(i+step < idxs.size() && idxs.get(i)+size > idxs.get(i+step)) step++;
i+=step;
}
return res;
}
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
String text = stdin.next();
int N = stdin.nextInt();
Vector<String> find_v = new Vector<String> ();
Vector<String> replace_v = new Vector<String> ();
for(int i=0;i<N;i++){
String buff = stdin.next();
find_v.add(buff);
}
for(int i=0;i<N;i++){
String buff = stdin.next();
replace_v.add(buff);
}
for(int i=0;i<N;i++){
KMP_String_Matching kmp = new KMP_String_Matching();
Vector<Integer> idxs = kmp.KMPSearch(find_v.get(i), text);
idxs = process_idxs(idxs, find_v.get(i).length());
for(int j=idxs.size()-1; j >= 0 ; j--){
text = text.substring(0, idxs.get(j)) + replace_v.get(i) + text.substring(idxs.get(j)+find_v.get(i).length());
}
}
System.out.println(text);
}
}
#include <iostream>
#include <vector>
#include <set>
#include <stack>
using namespace std;
int maxAreaOfIsland(vector<vector<int>>& grid) {
int max=0;
set<pair<int,int>> visited;
vector<pair<size_t,size_t>> moves = { {1,0}, {0,1}, {-1,0}, {0,-1} };
for(size_t i=0;i<grid.size();i++){
for(size_t j=0;j<grid[i].size();j++){
if(visited.find({i,j}) != visited.end() || grid[i][j] != 1) continue;
int count = 0;
stack<pair<int,int>> s;
s.push({i,j});
while(!s.empty()){
auto current = s.top(); s.pop();
if(visited.find(current) != visited.end() ) continue;
visited.insert(current);
// cout << current.first << " " << current.second << endl;
count++;
for(const auto &m:moves){
if(current.first + m.first >= 0 && current.first + m.first < grid.size() && current.second + m.second >=0 && current.second + m.second < grid[0].size() && grid[current.first+m.first][current.second+m.second]==1) {
s.push({current.first + m.first, current.second + m.second});
}
}
}
max = std::max(max, count);
}
}
return max;
}
int main(){
int M, N, B;
cin >> M >> N;
vector<vector<int>> grid(M);
for(int i =0;i<M;i++)
for(int j=0;j<N;j++) {
cin >> B;
grid[i].push_back(B);
}
cout << maxAreaOfIsland(grid) << endl;
}
import java.io.*;
import java.util.*;
public class Solution {
public static Vector<String> reorderLogFiles(Vector<String> logs) {
Vector<String> res = new Vector<String>();
int n = logs.size();
res.setSize(n);
int pLet = 0;
int pDig = n - 1;
for (int i = n-1; i >= 0; --i) {
String log = logs.get(i);
char first = log.charAt(log.indexOf(" ") + 1);
if (first >= '0' && first <= '9') {
res.set(pDig--,log);
} else {
res.set(pLet++,log);
}
}
Collections.sort(res.subList(0, pLet), new Comparator<String>() {
public int compare(String log1, String log2) {
int index1 = log1.indexOf(" ");
int index2 = log2.indexOf(" ");
return log1.substring(index1).compareTo(log2.substring(index2));
}
});
return res;
}
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in); // Create a Scanner object
int N = stdin.nextInt();
Vector<String> logs = new Vector<String>();
stdin.nextLine();
for(int i=0;i<N;i++){
logs.add(stdin.nextLine());
}
logs = reorderLogFiles(logs);
for(String log:logs){
System.out.println(log);
}
}
}
#include <stdio.h>
#include <vector>
#include <queue>
#include <memory.h>
#include <algorithm>
using namespace std;
int const N = 5 * 1e4 + 1;
long long const M = 1e18 + 1;
int n, m;
long long cost[N];
bool vis[N];
vector<vector<pair<int, int> > > g;
priority_queue<pair<int, int> > q;
void Dijkstra(int src) {
int v, c;
cost[src] = 0;
q.push(make_pair(0, src));
while(!q.empty()) {
v = q.top().second;
c = -q.top().first;
q.pop();
if(vis[v]) continue;
vis[v] = true;
for(int i = 0; i < g[v].size(); i++)
if(max(g[v][i].second, c) < cost[g[v][i].first])
if(g[v][i].second - c < 0) {
cost[g[v][i].first] = c;
q.push(make_pair(-c, g[v][i].first));
} else {
cost[g[v][i].first] = max(g[v][i].second, c);
q.push(make_pair(-(max(g[v][i].second, c)), g[v][i].first));
}
}
}
int main() {
scanf("%d%d", &n, &m);
g.clear();
g.resize(n);
for(int i = 0; i < n; i++) cost[i] = M;
memset(vis, false, sizeof vis);
int v, u, c;
while(m--) {
scanf("%d%d%d", &v, &u, &c);
v--; u--;
g[v].push_back(make_pair(u, c));
g[u].push_back(make_pair(v, c));
}
Dijkstra(0);
if(cost[n-1] == M) puts("NO PATH EXISTS");
else printf("%lld\n", cost[n-1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment