Advertisement
Pug_coder

TASK5

Jun 6th, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.02 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. var qs []int
  8. var ys []string
  9. type AutoMealy struct{
  10.     n, m, m1 int
  11.     inputAlphabet, outputAlphabet []string
  12.     crossover [][]int
  13.     exit [][]string
  14.     ex []string
  15. }
  16.  
  17. func Print(crossover [][]int, indices []int, mealy AutoMealy)  {
  18.     fmt.Println("digraph {")
  19.     fmt.Println("rankdir = LR")
  20.     for i, _ := range indices {
  21.         fmt.Printf("%d [label = \"(%d,%s)\"]\n", i, qs[indices[i]], ys[indices[i]])
  22.     }
  23.     for i, _ := range crossover {
  24.  
  25.         for j, x := range crossover[i] {
  26.             fmt.Printf("%d -> %d [label = \"%s\"]\n", i, x, mealy.inputAlphabet[j])
  27.         }
  28.     }
  29.     fmt.Println("}")
  30. }
  31.  
  32. func Search(indices []int)  int {
  33.     for i, s := range indices {
  34.         if qs[len(qs) - 1] == qs[s] && ys[len(qs) - 1] == ys[s] {
  35.             return i
  36.         }
  37.     }
  38.     return -1
  39. }
  40. func partiton( low int, high int, less func(i, j int )bool, swap func(i, j int)) int{
  41.     i := low
  42.     for j := low; j < high; j++ {
  43.         if less(j,high) {
  44.             swap(i,j)
  45.             i++
  46.         }
  47.     }
  48.     swap(i,high)
  49.     return i
  50. }
  51. func quickSortRec(low int, high int, less func(i, j int) bool, swap func(i, j int)){
  52.     if low < high{
  53.         q := partiton(low, high, less ,swap)
  54.         quickSortRec(low , q - 1, less, swap)
  55.         quickSortRec( q + 1, high, less, swap)
  56.     }
  57. }
  58. func QuickSort(n int, less func(i, j int) bool,swap func(i, j int)) {
  59.     quickSortRec(0, n - 1, less, swap)
  60. }
  61. func (newAuto *AutoMealy) NewMealy() *AutoMealy{
  62.     var m,m1,n int
  63.     fmt.Scan(&m)
  64.     newAuto.m = m
  65.     inputAlphabet := make([]string, m)
  66.     for i := 0; i < m; i++ {
  67.         fmt.Scan(&inputAlphabet[i])
  68.     }
  69.     fmt.Scan(&m1)
  70.     newAuto.m1 = m1
  71.     newAuto.inputAlphabet = inputAlphabet
  72.     outputAlphabet := make([]string, m1)
  73.     for i := 0; i < m1; i++ {
  74.         fmt.Scan(&outputAlphabet[i])
  75.     }
  76.     fmt.Scan(&n)
  77.     newAuto.n = n
  78.     newAuto.outputAlphabet = outputAlphabet
  79.     crossover := make([][]int, n)
  80.  
  81.     for i := 0; i < n; i++ {
  82.         crossover[i] = make([]int, m )
  83.         for j := 0; j < m; j++ {
  84.             fmt.Scan(&crossover[i][j])
  85.         }
  86.     }
  87.     newAuto.crossover = crossover
  88.     exit := make([][]string, n)
  89.     for i := 0; i < n; i++ {
  90.         exit[i] = make([]string, m)
  91.         for j := 0; j < m; j++ {
  92.             fmt.Scan(&exit[i][j])
  93.         }
  94.     }
  95.     newAuto.exit = exit
  96.     return newAuto
  97. }
  98. func main() {
  99.     var mealy AutoMealy
  100.     mealy.NewMealy()
  101.     // Составим массив состояний Мура
  102.     states := make([]int, 0)
  103.     for i := range mealy.crossover {
  104.         for j, x := range mealy.crossover[i] {
  105.             qs = append(qs, x)
  106.             ys = append(ys, mealy.exit[i][j])
  107.             if s := Search(states); s == -1 {
  108.                 states = append(states, j + i * mealy.m)
  109.             }
  110.         }
  111.     }
  112.     if len(states) > 1 {
  113.         QuickSort(len(states), func(i, j int) bool {
  114.             return states[i] < states[j]
  115.         },
  116.             func(i, j int) { states[i], states[j] = states[j], states[i] },
  117.         )
  118.     }
  119.     newCrossover := make([][]int, len(states))
  120.     for i := 0; i < len(states); i++ {
  121.         newCrossover[i] = make([]int, mealy.m)
  122.         for j := 0; j < mealy.m; j++ {
  123.             qs = append(qs, mealy.crossover[qs[states[i]]][j])
  124.             ys = append(ys, mealy.exit[qs[states[i]]][j])
  125.             newCrossover[i][j] = Search(states)
  126.         }
  127.     }
  128.  
  129.     Print(newCrossover, states, mealy)
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement